0

sample、、、の3つのテーブルを持つMySQLデータベースがありmethodますcompound

sample次の列があります:id(PK)(int)、、、、、、、date(date)compound_id(int)location(varchar)method(int)value(float)

method次の列があります:id(PK)(int)label(varchar)

そして持ってcompoundいる:id(PK)(int)、、name(varchar)unit(varchar)

次の基準で一意の行のみを取得するSQLコマンドを生成しようとしています。

  • 日付(sample.date
  • 化合物名(compound.name
  • 場所(sample.location
  • 方法(sample.method

sampleただし、いくつかの列のラベルを数字ではなく置き換えたいと思います。

  • sample.compound_id対応するとcompound.idを持っているに一致しますcompound.namecompound.unit

私が照会しようとした最初のSQLコマンドは次のとおりです。

SELECT sample.id, sample.date, compound.name, sample.location, method.label, sample.value, compound.unit
FROM sample, compound, method 
WHERE sample.date = "2011-11-03"
AND compound.name = "Zinc (Dissolved)"
AND sample.location = "13.0"
AND method.id = 1;

上記のコマンドからの出力:

id      date        name            location    label       value   unit
1       2011-11-03      Zinc (Dissolved)    13.0        (1) Indivi...   378.261     μg/L
5       2011-11-03      Zinc (Dissolved)    13.0        (1) Indivi...   197.917     μg/L
9       2011-11-03      Zinc (Dissolved)    13.0        (1) Indivi...   92.4051     μg/L

しかし、私が返されたものを見てsample比較すると、次のようになります。sample.id

id      date    compound_id     location     method     value   
1       2011-11-03  13          13.0         1          378.261
5       2011-11-03  14          13.0         1          197.917
9       2011-11-03  47          13.0         1          92.4051

ここでcompound.id、47はcompound.id47およびcompound.name「亜鉛(溶解)」に対応します。化合物ID#13および#14は、それぞれ「銅(溶解)」および「銅(合計)」です。

したがって、に関係なく、の基準を満たす行を返しているようsample.dateです。上記の基準を考えると、データベースは1つの行のみを返す必要があることを知っていますが、代わりに、指定した一致とは完全に異なる行をいくつか取得します。sample.locationcompound.namesample.idsample.compound_idcompound.name

最初の行で編集された列がSELECT、私が書いたのと同じ順序で終わるようにしたいと思います。このコードは、私がPython / Tkinterで書いている小さなデータベースビューア/レポータープログラム用であり、列が均一であることに依存しています。プログラムのデータを初期化するために使用するコードは、期待どおりに機能します。

SELECT sample.id, sample.date, compound.name, sample.location, method.label, sample.value, compound.unit
FROM sample, compound, method 
WHERE sample.compound_id = compound.id
AND sample.method = method.id;

これは、 toとtosampleの置換を使用して各一意の行を出力し、最後にを追加します。sample.compound_idcompound.namesample.methodmethod.labelcompound.unit

質問1:特定の条件を満たす行のみを返すようにクエリを再構築するにはどうすればよいですか?

質問2:最終的には一度に複数を指定する必要がありますsample.locationsORそれは、必要な個々の場所ごとにステートメントを追加するのと同じくらい簡単ですか?

4

2 に答える 2

2
SELECT sample.id, sample.date, compound.name, sample.location, method.label, sample.value, compound.unit
FROM sample 
INNER JOIN compound ON compound.id = sample.compound_id 
INNER JOIN method ON method.id = sample.method
WHERE sample.date = '2011-11-03'
AND compound.name = 'Zinc (Dissolved)'
AND sample.location = "13.0"
AND method.id = 1;
于 2013-01-24T17:21:42.033 に答える
1

最初の質問を理解したので、2 番目の質問を理解しました。

SELECT sample.id, sample.date, compound.name, sample.location, method.label, sample.value, compound.unit
FROM sample 
INNER JOIN compound ON compound.id = sample.compound_id 
INNER JOIN method ON method.id = sample.method
WHERE sample.date = '2011-11-03'  
AND compound.name = 'Zinc (Dissolved)'
AND sample.location IN ("13.0", "22.0")
AND method.id = 1;

そしてOR、他の場所ごとに括弧内に s を追加し続けます。

于 2013-01-24T17:30:24.543 に答える