我有一个名为lottery_winners
的表,其中包含以下有用的列:
+----+------+------+--------+---------+ | id | plid | zbid | amount | numbers | +----+------+------+--------+---------+
id
是表的唯一主要ID。 plid
是指past_lotteries
表(即我可以通过这种方式从特定的彩票中获得所有彩票中奖者。zbid
是会员/用户的ID(获胜者) 。amount
是他们在彩票中赢得的金额,最后numbers
是VARCHAR
CSV字段及其彩票号码。
以下是表格中可以包含哪些行的示例:
+----+------+------+--------+---------+ | 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 | +----+------+------+--------+---------+
现在,我想运行一个SELECT
语句,它会以非常特定的顺序返回所有行。这些行应按zbid
分组(在本例中我添加了WHERE plid=1
子句):
+----+------+------+--------+---------+ | 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 | +----+------+------+--------+---------+
下一个标准是,它们不仅应按zbid
分组,而且在此分组中,它们应按amount DESC
排序。这就是它现在的样子:
+----+------+------+--------+---------+ | 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 | +----+------+------+--------+---------+
前两行已交换过。
还有一个标准。正如您所看到的,尽管它们按zbid
分组,但它们没有特定的顺序。我希望它按zbid
分组,但订单应基于每个组的sum(amount)
。
下表按特定顺序显示了每个zbid
的总计(考虑到plid=1
:
+------+-------------+ | zbid | sum(amount) | +------+-------------+ | 2 | 4 | +------+-------------+ | 3 | 7 | +------+-------------+ | 4 | 5 | +------+-------------+
因此,使用此信息时,使用SELECT
语句的最终结果应为以下内容(添加了sum(amount)
列):
+----+------+------+--------+---------+-------------+ | 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 | +----+------+------+--------+---------+-------------+
就是这样!现在我自己尝试了几件事,但我不确定如何获得完整的最终结果。我试过了:
SELECT id,plid,zbid,amount,numbers,sum(amount) FROM lottery_winners GROUP BY zbid ORDER BY sum(amount) DESC
现在这似乎符合最终标准,但它没有给我个人的结果。
另请注意,由于这些结果将进行分页,因此我需要在查询末尾添加LIMIT $start,$perpage
。
答案 0 :(得分:2)
尝试一下:
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