mysql选择表中不存在的行,没有值

时间:2014-12-23 04:40:46

标签: mysql left-join

我有2张桌子

表1:

+-------------------------+--------+
| Playername              | Gender |
+-------------------------+--------+
| Kendall dddddd          | Female |
| Ivy xxxxxxxx            | Female |
| Carson xxxxxxx          | Female |
+-------------------------+--------+

表2:

+----------------+----------+
| Playername     | EvalYear |
+----------------+----------+
| Kendall dddddd |     2014 |
| Carson xxxxxxx |     2013 |
+----------------+----------+

查询

SELECT table1.Playername, table1.Gender, table2.EvalYear
FROM table1 
LEFT JOIN table2 ON table1.Playername = table2.PlayerName
where table2.EvalYear is Null and table2.EvalYear != '2014'

我需要查询返回 Ivy Carson 名称。

2 个答案:

答案 0 :(得分:1)

在执行LEFT JOIN时,您应该在ON子句中的第二个表中放置必须匹配的条件,而不是WHERE子句。由于您返回的行与年份不匹配,因此您需要更改测试的意义。

SELECT table1.Playername, table1.Gender, table2.EvalYear
FROM table1 
LEFT JOIN table2 ON table1.Playername = table2.PlayerName AND table2.EvalYear = 2014

结果:

+----------------+--------+----------+
| Playername     | Gender | EvalYear |
+----------------+--------+----------+
| Kendall dddddd | Female |     2014 |
| Ivy xxxxxxxx   | Female |     NULL |
| Carson xxxxxxx | Female |     NULL |
+----------------+--------+----------+

答案 1 :(得分:0)

<强>查询

SELECT table1.Playername, table1.Gender, table2.EvalYear
FROM table1 
LEFT JOIN table2 ON table1.Playername = table2.PlayerName
where PlayerEvals.EvalYear is Null or PlayerEvals !='2014'