简体   繁体   中英

MySQL select * from one table based on stock from another table

I have two tables oos_notify and temp_parts .

The oos_notify table stores user information who signed up to recieve stock updates for a out of stock part. In the oos_notify table I am storing the users email , part_id , datetime and email_sent check field.

The temp_parts table stores part information with part_id , part_name and stock . (This is simplified).

What I am trying to do is select all the data from the oos_notify table only if the corresponding part_id in that table is in stock ( temp_parts.stock > 0 ) from the temp_parts table.

This is what I have but it is returning all the oos_notify data which is wrong

SELECT * FROM oos_email_notify 
JOIN temp_parts ON temp_parts.code = oos_email_notify.part
WHERE temp_parts.stock >'0'
AND email_sent ='0'

Would it be possible to have some help on this. Thanks

If I'm not mistaken, the link between the 2 tables is part_id, instead of code.
Try this.

SELECT * FROM oos_email_notify tbl1
JOIN temp_parts tbl2
ON tbl1.part_id = tbl2.part_id
WHERE tbl2.stock > 0
AND tbl1.email_sent = 0

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