Mysql 从具有多个条件的多个表中选择

Mysql 从具有多个条件的多个表中选择,mysql,sql,Mysql,Sql,我有一个MySQL数据库,它有三个表:sample,method,component sample有以下列:id(PK)(int),date(date),composite\u id(int),位置(varchar),方法(int),值(float) 方法具有以下列:id(PK)(int),label(varchar) 化合物有:id(PK)(int),名称(varchar),单位(varchar) 我正在尝试生成一个SQL命令,该命令仅针对以下条件拉入唯一行: 日期(sample.Date)

我有一个MySQL数据库,它有三个表:
sample
method
component

sample
有以下列:
id(PK)(int)
date(date)
composite\u id(int)
位置(varchar)
方法(int)
值(float)

方法具有以下列:
id(PK)(int)
label(varchar)

化合物有:
id(PK)(int)
名称(varchar)
单位(varchar)

我正在尝试生成一个SQL命令,该命令仅针对以下条件拉入唯一行:

  • 日期(
    sample.Date
  • 复合名称(
    composite.Name
  • 位置(
    sample.Location
  • 方法(
    sample.Method
但是,我想在标签中替换一些
sample
列,而不是数字:

  • sample.component\u id
    component.id
    匹配,后者具有相应的
    component.name
    component.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
其中,
component.id
47对应于
component.id
47和
component.name
“锌(溶解)”。化合物ID#13和#14分别为“铜(溶解)”和“铜(总)”

因此,它似乎返回符合
sample.date
sample.location
标准的行,而不考虑
composite.name
。根据上述条件,我知道我的数据库应该只返回一行,但是我得到的是一些
sample.id
行,它们与我指定的匹配的
component.name
具有完全不同的
sample.component\u id

我希望以第一行中的
SELECT
ed列结束,以与我编写它们相同的顺序结束。这段代码是为我正在用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;
它在
sample
中列出每个唯一的行,并替换
sample.component\u id
component.name
sample.method
method.label
并在末尾添加到
component.unit

问题#1:我需要如何重新构造查询,使其只返回满足该特定条件的行


问题2:最终我需要一次指定多个
sample.location
。这就像为我需要的每个位置添加一个
语句一样简单吗?

既然我已经解决了第一个问题,我就解决了第二个问题:

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;
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;

只需在括号内为每个位置添加
s即可。

现在我已经解决了第一个问题,我也解决了第二个问题:

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;

只需在括号内为每个其他位置添加
s即可。

对于第一个查询-不符合条件的查询-缺少sample.comapund_d=component.id等信息的连接。另外,您可能需要检查复合词的拼写。名称值或考虑使用复合词。名称“Z%”和IANCU:我是SQL哑。我不确定Inner连接语法如何处理这么多的条件。对于第一个查询(不符合条件的查询),sample.comapund\u d=component.id等信息缺少连接。另外,您可能需要检查复合词的拼写。名称值或考虑使用复合词。名称“Z%”和IANCU:我是SQL哑。我不确定Inner JOIN语法在这么多的条件下如何工作。我认为这将是一个简单的解决方案!这就成功了。我想这是一个简单的解决办法!这就是诀窍。use IN子句:和sample.location IN(“13.0”、“22.0”)和a Iancu:看起来这两种方法都有效。IN子句是否被认为更合适(并且可能更容易阅读)SQL?是的,这两种方法都有效-是首选方法(或者更容易阅读)-如果您有大约200个位置,比如200个或语句;在子句中使用:和在(“13.0”、“22.0”)中使用sample.location和a Iancu:看起来这两种方法都有效。IN子句是否被认为更合适(并且可能更容易阅读)SQL?是的,这两种方法都有效-是首选方法(或者更容易阅读)-如果您有大约200个位置,比如200个或语句;