« first day (2027 days earlier)      last day (631 days later) » 

11:03 AM
Could you explain how this MySQL query to get first n rows from every category in a table works? (refer link above)
Also, please advise which site of stackexchange network does this fit in. Afraid of downvotes :D
 
 
4 hours later…
3:32 PM
@Alex- After you get the two databases exactly in sync, you have to run a command to say "skip all those other queries you have queued up, and only start running the queries after this point"
@SandeepanNath That's difficult to say, because it's not a whole question
@SandeepanNath it's written with SQL, it uses the SELECT, FROM, JOIN, WHERE, and GROUP BY clauses, and it also makes use of a subquery
It could be rewritten without using a subquery
@SandeepanNath that query does not get the first N rows for every category
oh no, I see what it's doing. Yeah, it's getting the first and second sales by primary key order in each category
actually first, second, and third, thanks to that COUNT
The easiest way to visualize what it's doing is to run parts of the query yourself
First, run
SELECT t1.*
FROM sales t1
LEFT JOIN sales t2
  ON t2.category = t1.category AND t2.id <= t1.id
Then, run
SELECT t1.*, COUNT(*) cnt
FROM sales t1
LEFT JOIN sales t2
  ON t2.category = t1.category AND t2.id <= t1.id
GROUP BY t1.id
 

« first day (2027 days earlier)      last day (631 days later) »