我正在尝试只在我加入的表中的一列= 1时才进行LEFT JOIN。
这是我到目前为止所拥有的。
SELECT * FROM size, oil LEFT JOIN purchase ON oil. oilId = purchase. purOilId
WHERE sizId = oilSizeId AND oilUnused ='' ORDER BY oilOurName
我需要添加的内容如下。
LEFT JOIN purhcase (only if purMark = 1)
这是我不知道该怎么办。
以后使用AND是行不通的,因为油表需要定义结果中有多少行。
由于
答案 0 :(得分:2)
将AND条件添加到JOIN,如下所示
SELECT * FROM size, oil LEFT JOIN purchase ON oil.oilId = purchase.purOilId AND purchase.purMark = 1
WHERE sizId = oilSizeId AND oilUnused ='' ORDER BY oilOurName
答案 1 :(得分:1)
在mysql中,我认为你能做到这一点的唯一方法是在你的连接中使用一个子查询,类似于:
SELECT * FROM size, oil
LEFT JOIN (
SELECT *
FROM purchase
WHERE purMark = 1
) purchase ON purchase.purOilId = oil.oilId /* or whatever your joining criteria is */
WHERE sizId = oilSizeId AND oilUnused =''
ORDER BY oilOurName
希望这有帮助。
答案 2 :(得分:0)
尝试以下:
SELECT * FROM size, oil
LEFT JOIN purchase ON oil. oilId = purchase. purOilId
WHERE purMark = 1
sizId = oilSizeId AND oilUnused =''
UNION
SELECT * FROM size, oil
WHERE purMark != 1
sizId = oilSizeId AND oilUnused =''
或者
SELECT * FROM size, oil
LEFT JOIN purchase ON oil. oilId = purchase. purOilId AND purMark = 1
WHERE sizId = oilSizeId AND oilUnused =''
答案 3 :(得分:0)
奇怪的是,比这里晚了一周,here提出并回答了这个问题