I have a table called 'account_products', which have a foreign key 'product_id' and also a timestamps 'created_at'.
I also have a table called 'products'. I'm trying to return all the products that has the id equal to the same 'product_id' from the 'accout_products'. BUT I also need the 'created_at' from the 'account_products', because I want to see when they were created.
So is there a way to tell the SQL something like:
SELECT * FROM products
WHERE id IN (
SELECT product_id, created_at FROM account_products
)
but the IN clause only relate to product_id, also returning the created_at for each column of account_products?
Thanks!
Use a join
:
SELECT p.*, ap.created_at
FROM products p JOIN
account_products ap
ON ap.product_id = p.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.