SQL Join:选择第二个表中没有匹配的行

时间:2016-02-15 01:27:53

标签: mysql join

我正在尝试连接两个表,同时仍然从表A中获得结果,即使表B中没有匹配的记录。我认为where子句导致问题,但我似乎无法修复它。

SELECT a.id, a.title, a.validation, b.data 
FROM client_option_detail AS a LEFT OUTER JOIN client_data AS b 
ON a.id = b.client_option_detail_id 
WHERE a.client_option_id = ?
AND (b.client_id IS NULL OR b.client_id = ?)

更新: 数据库布局

client_option       client_option_detail 
-------------       ------------------- 
id                  id
                    title
                    validation          
                    client_option_id


client          client_data
-------         ------------ 
id              client_id
                client_option_detail_id             
                data

数据库示例:

client_option       client_option_detail 
-------------      ------------------------------------------- 
id                  id | title | validation | client_option_id
-------------      -------------------------------------------
1                  1   | test1 |            | 1  
2                  2   | test2 |            | 1                               
                   3   | test3 |            | 1
                   4   | test4 |            | 2


client              client_data (primary key - client_id + client_option_detail_id)
--------            ------------------------------------------- 
 id                 client_id | client_option_detail_id | data
--------            -------------------------------------------
 1                  1         | 1                       | data1
 2                  1         | 2                       | data2
                    1         | 4                       | data3
                    2         | 3                       | data4
                    2         | 1                       | data5

示例查询:

SELECT a.id, a.title, a.validation, b.data 
FROM client_option_detail AS a LEFT OUTER JOIN client_data AS b 
ON a.id = b.client_option_detail_id 
WHERE a.client_option_id = 1
AND (b.client_id IS NULL OR b.client_id = 1)

必需的输出:

    ------------------------------------------- 
     id | title | validation | data
    -------------------------------------------
     1  | test1 |            | data1                                 
     2  | test2 |            | data2
     3  | test3 |            | 

2 个答案:

答案 0 :(得分:0)

SELECT client_option_detail.id, title, validation, data 
FROM client_option_detail LEFT OUTER JOIN client_data 
ON client_option_detail.id = client_data.client_option_detail_id 

应该是好的。我认为你的情况有问题。
例如:client_option_detail.client_option_id =? ,?它是否包含client_option_detail中的所有id?

答案 1 :(得分:0)

您不需要AND (b.client_id IS NULL OR b.client_id = ?)

SELECT a.id, a.title, a.validation, b.data 
FROM client_option_detail AS a 
LEFT JOIN client_data AS b 
ON a.id = b.client_option_detail_id 
WHERE a.client_option_id = ?

输出将类似于:

id     title     validation     data
1      foo       bar            sample_data1
2      apple     oranges        null
3      try       it             sample_data2

与样本输出编号2类似,如果表B中没有匹配,则数据将为空,并且仍然会显示表A中的信息

另外,如果你真的想根据B中指定的client_id进行过滤,那么就像下面的最后一部分一样。这意味着如果数据包含来自B的指定client_id或者如果它具有来自A的指定client_option_id,则将检索它。

SELECT a.id, a.title, a.validation, b.data 
FROM client_option_detail AS a 
LEFT JOIN client_data AS b 
ON a.id = b.client_option_detail_id 
WHERE a.client_option_id = ? OR b.client_id = ?

有关详细信息,您可能需要查看this post

<强>更新

请尝试以下代码进行更新:

SELECT a.id, a.title, a.validation, b.data 
FROM client_option_detail AS a 
LEFT JOIN client_data AS b 
ON a.id = b.client_option_detail_id AND b.client_id = a.client_option_id
WHERE a.client_option_id = 1

或者,我不知道哪个是最合适的,哪个最适合您的需求,但两者都会根据您的样本数据产生相同的结果:

SELECT a.id, a.title, a.validation, b.data 
FROM client_option_detail AS a 
LEFT JOIN client_data AS b 
ON a.id = b.client_option_detail_id AND b.client_id = 1
WHERE a.client_option_id = 1

尝试使用SQLize:

enter image description here

我希望这次能说得对。祝你好运