我需要使用case条件修改这个sql,我需要所有模型来回显但是如果where条件满足则只回显tw。最好的方法是什么?
SELECT * FROM model
LEFT OUTER JOIN tw
ON model.model_name=tw.model
WHERE tw.completed = 1 AND tw.stock = 0
ORDER BY model.id
答案 0 :(得分:1)
我认为您只需将where
条件移至on
子句:
SELECT m.*, t2.*
FROM model m LEFT OUTER JOIN
tw
ON m.model_name = tw.model AND
tw.completed = 1 AND tw.stock = 0
ORDER BY m.id;