我需要加入2个表,但这些表有两对需要连接的键。
ID,Car和Color是一个“对”而不是OTHERID
。
LEFT JOIN TableTest t2 ON
(t1.ID = t2.ID)
AND
(t1.CAR = t2.CAR)
AND (t1.Color = t2.Color)
我现在尝试将OTHERID
键添加到语句中:
LEFT JOIN TableTest t2 ON
(t1.ID = t2.ID)
AND
(t1.CAR = t2.CAR)
AND
(t1.Color = t2.Color)
OR
(t1.OTHERID = t2.OTHERID);
但那不起作用。
答案 0 :(得分:1)
您忽略了AND
vs OR
的运算符优先级。
如果t1.OTHERID = t2.OTHERID
应该首先出现,那么就这样推广它(你也不需要为每个表达式加上括号):
LEFT JOIN TableTest t2 ON
t1.OTHERID = t2.OTHERID
OR
(
t1.ID = t2.ID
AND
t1.CAR = t2.CAR
AND
t1.Color = t2.Color
);
答案 1 :(得分:0)
小心括号。
LEFT JOIN TableTest t2 ON (t1.ID = t2.ID AND t1.CAR = t2.CAR AND t1.Color = t2.Color) OR t1.OTHERID = t2.OTHERID