当一个表返回0时,mysql返回匹配其他行

时间:2014-01-01 15:29:55

标签: mysql subquery

简单地说:

SELECT
  table1.field1,
  table2.field2,
  table3.field_WHAT
FROM
  table1,
  table2,
  table3
WHERE
  table1.field1 = 'HELP'
  and table2.field1 = table1.field1
  and table3.field2 = table2.field2

我仍然想要table1&的所有匹配行table2,即使找不到table3.field2的匹配记录...而理想情况如果没有匹配,table3.field_WHAT将返回""NULL

2 个答案:

答案 0 :(得分:0)

试试这个:

SELECT
  table1.field1,
  table2.field2,
  table3.field_WHAT
FROM
  table1
INNER JOIN
  table2 ON table1.field1 = table2.field1
LEFT OUTER JOIN
  table3 ON table3.field2 = table2.field2
WHERE
  table1.field1 = 'HELP'

答案 1 :(得分:0)

这是outer join的经典案例:

SELECT
  table1.field1,
  table2.field2,
  table3.field_WHAT
FROM
  table1,
JOIN 
  table2 ON table2.field1 = table1.field1
OUTER JOIN
  table3 ON table3.field2 = table2.field2
WHERE
  table1.field1 = 'HELP'