3

仅当我要加入的表中的一列 = 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 是行不通的,因为油表需要定义结果中有多少行。

谢谢

4

4 回答 4

3

向 JOIN 添加 AND 条件,如下所示

SELECT * FROM size, oil LEFT JOIN purchase ON oil.oilId = purchase.purOilId AND purchase.purMark = 1  
WHERE sizId = oilSizeId AND oilUnused ='' ORDER BY oilOurName
于 2013-07-03T04:21:00.737 回答
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

希望这可以帮助。

于 2013-07-03T04:22:58.370 回答
1

奇怪的是,这个问题比这里晚了一周被问回答

于 2019-05-14T14:20:55.540 回答
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 ='' 
于 2013-07-03T04:25:12.857 回答