mysql - 使用 GROUP BY 获取单个值 - MySQL

标签 mysql select group-by sql-order-by

我有一个名为 lottery_winners 的表,其中包含以下有用的列:

+----+------+------+--------+---------+
| 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

现在这似乎满足了最终标准,但它没有给我表格的单独结果。

另请注意,由于这些结果将被分页,因此我需要将 LIMIT $start,$perpage 添加到查询末尾。

最佳答案

尝试一下:

SELECT  a.id, 
        a.plid, 
        a.zbid,
        a.amount, 
        a.numbers,
        c.totalAmount
FROM    lottery_winners a
            INNER JOIN  (
                            SELECT  b.zbid,
                                    SUM(b.amount) totalAmount
                            FROM    lottery_winner b
                            WHERE   b.plid = 1
                            GROUP BY b.zbid
                        ) c 
                ON a.zbid = c.zbid
WHERE   a.plid = 1
ORDER BY    c.totalAmount desc

关于mysql - 使用 GROUP BY 获取单个值 - MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11797078/

相关文章:

mysql SELECT * FROM 表 WHERE 列+空格+列 LIKE '%query%'

mysql - ColdFusion 动态文本区域

css - 如何确保选择选项文本在 IE 中居中对齐?

MySQL 如何使用 MIN 进行更新

MySQL:如何获取计数> 1 的记录?

python - Pandas dataframe 多重 groupby 过滤

python - Pandas - 在 groupby 之后将列转换为新行

sql - 在 SQLite 中组合两列的巧妙方法?

mysql - 如何为 MySQL 模型制作 GraphQL 模式?

mysql - Jaro-winkler 函数 : why is the same score matching very similar and very different words?