简体   繁体   中英

MySQL Select multiple distinct

I have one table which holds lots of records. Its for an auction site. This table can have multiple user_id which can be the same and multiple auction_id which can be the same.

I am trying to write a script that sends an email once to each user that has placed a bid. If a user places say 10 bids on the same auction, I only want the email to be sent once per user per auction, per bid.

How would I do this with distinct over 2 fields? The user_id field and the auction_listing field? I will also need to have a WHERE clause so I only select records of auctions that have less than 24 hours to run.

SELECT DISTINCT b.user_id, b.auction_listing
FROM AuctionBids b
JOIN Auctions a USING (auction_listing)
WHERE a.end_datetime < NOW() + INTERVAL 24 hour

Or

SELECT b.user_id, b.auction_listing
FROM AuctionBids b
JOIN Auctions a USING (auction_listing)
WHERE a.end_datetime < NOW() + INTERVAL 24 hour
GROUP BY b.user_id, b.auction_listing

Without knowing your table schema it is hard to answer to answer you question.

In general if you do a GROUP_BY user_id in the end one user will only appear once in your query results

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