I have a table called lottery_winners with the following useful colums:
+----+------+------+--------+---------+ | id | plid | zbid | amount | numbers | +----+------+------+--------+---------+
id is the unique, primary id for the table. plid refers to the past_lotteries table (i.e I can get all the lottery winners from a specific lottery in the past this way. zbid is the id of the member/user (the winner). amount is the sum of money they won in the lottery, and finally numbers is a VARCHAR CSV field with their lottery numbers.
Here’s an example of what rows could be in the table:
+----+------+------+--------+---------+ | id | plid | zbid | amount | numbers | +----+------+------+--------+---------+ | 1 | 1 | 2 | 1 | 1,2,3 | +----+------+------+--------+---------+ | 2 | 1 | 4 | 5 | 4,5,6 | +----+------+------+--------+---------+ | 3 | 1 | 3 | 7 | 3,4,5 | +----+------+------+--------+---------+ | 4 | 1 | 2 | 3 | 7,8,9 | +----+------+------+--------+---------+ | 5 | 2 | 2 | 8 | 8,9,10 | +----+------+------+--------+---------+
Now, I want to run a SELECT statement which will bring back all the rows but in a really specific order. The rows should be in grouped by zbid as such (in this case I have added a WHERE plid=1 clause):
+----+------+------+--------+---------+ | id | plid | zbid | amount | numbers | +----+------+------+--------+---------+ | 1 | 1 | 2 | 1 | 1,2,3 | +----+------+------+--------+---------+ | 4 | 1 | 2 | 3 | 7,8,9 | +----+------+------+--------+---------+ | 2 | 1 | 4 | 5 | 4,5,6 | +----+------+------+--------+---------+ | 3 | 1 | 3 | 7 | 3,4,5 | +----+------+------+--------+---------+
Next criteria is that not only should they be grouped by zbid, but within this grouping they should be ordered by amount DESC. This is what it would now look like:
+----+------+------+--------+---------+ | id | plid | zbid | amount | numbers | +----+------+------+--------+---------+ | 4 | 1 | 2 | 3 | 7,8,9 | +----+------+------+--------+---------+ | 1 | 1 | 2 | 1 | 1,2,3 | +----+------+------+--------+---------+ | 2 | 1 | 4 | 5 | 4,5,6 | +----+------+------+--------+---------+ | 3 | 1 | 3 | 7 | 3,4,5 | +----+------+------+--------+---------+
The top two rows have swapped around.
One more criteria. As you can see, although they are grouped by zbid, there’s no specific order to them. I want it to group by zbid, but the order should be based on sum(amount) for each group.
The following table shows the totals for each zbid in no specific order (taking into account that plid=1:
+------+-------------+ | zbid | sum(amount) | +------+-------------+ | 2 | 4 | +------+-------------+ | 3 | 7 | +------+-------------+ | 4 | 5 | +------+-------------+
So using this information the final result using the SELECT statement should be the following (with an added sum(amount) column):
+----+------+------+--------+---------+-------------+ | id | plid | zbid | amount | numbers | sum(amount) | +----+------+------+--------+---------+-------------+ | 3 | 1 | 3 | 7 | 3,4,5 | 7 | +----+------+------+--------+---------+-------------+ | 2 | 1 | 4 | 5 | 4,5,6 | 5 | +----+------+------+--------+---------+-------------+ | 4 | 1 | 2 | 3 | 7,8,9 | 4 | +----+------+------+--------+---------+-------------+ | 1 | 1 | 2 | 1 | 1,2,3 | 4 | +----+------+------+--------+---------+-------------+
That’s it! Now I’ve tried a couple of things myself, but I’m not exactly sure how to get the full final result. I have tried:
SELECT id,plid,zbid,amount,numbers,sum(amount) FROM lottery_winners GROUP BY zbid ORDER BY sum(amount) DESC
Now that seemed to meet the final criterion, but it didn’t give me individual results for the table.
Please also note that as these results will be paginated, I will need to be adding LIMIT $start,$perpage to the end of the query.
have this a try: