简体   繁体   中英

How to query a MySql table with a where clause from another table?

I have two tables in a MySql database, one called sales and one called auctions. The sales table has an auction_id column. I want to extract sales which were only made in EUR, but the currency is a column in the auctions table. What query should I run to achieve this? Many thanks

You can do this using inner query

select * 
from sales 
where auction_id in (select id from auctions where currency='EUR');

what it will do

  • first it will run inner query and select all id having currency EUR.
  • after it will check if sales contain the auction_id which being selected from inner query

Other option is also available that is using join

SELECT * 
FROM sales, auctions 
WHERE sales.auction_id = auctions.id AND auctions.currency='EUR'

OR

SELECT * 
FROM sales 
JOIN auctions ON sales.auction_id = auctions.id AND auctions.currency='EUR'

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