我有一个关于mysql中元组比较的问题。
我有两张桌子:
Person_ID | Class_ID | STATE
1 | 1 | 1
1 | 3 | 0
Person_ID | Class_ID
1 | 1
1 | 2
1 | 3
因此,第一行中不存在第二行。但是,我希望将这些表组合起来:
Person_ID | Class_ID | STATE
1 | 1 | 1
1 | 2 | -1
1 | 3 | 0
有人在想,我该怎么做?
我尝试使用存在,if,where等: - (。
答案 0 :(得分:0)
您可以LEFT JOIN
使用COALESCE
:
SELECT t2.Person_ID, t2.Class_ID,
COALESCE(t1.STATE,-1) AS STATE
FROM Table2 AS t2
LEFT JOIN Table1 AS t1
ON t2.Person_ID = t1.Person_ID AND t2.Class_ID = t1.Class_ID