简体   繁体   中英

SQL Case Condition on joined table

I need to modify this sql using a case condition, i need all of model to echo but only echo tw if the where condition meets. What would be the best way to do this?

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

I think you simply need to move the where condition to the on clause:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM