I’m having trouble understanding this query:
SELECT *
FROM `advertise` parent
WHERE 3 <= ( SELECT COUNT(username)
FROM `advertise`
WHERE keyword = parent.keyword
AND bid > parent.bid)
AND username = 'mike'
What this query does is display any rows with the username 'mike'; if that row is not in the three highest bids for a keyword.
Here is the sqlfiddle that works, but I don’t understand how it works. More specifically I don’t understand 3 <= with this specific query.
How does 3 <= determine the row is not in the three highest bids for a keyword?
This query works like so:
The subquery
finds the number of rows that have a higher bid for the same keyword. We then specify that the bid we are looking for cannot be in the top three bids, so we require that at least 3 bids be returned from that query. Here is what I think your end query should look like: