Mysql 返回按日期划分的行的SQL查询

Mysql 返回按日期划分的行的SQL查询,mysql,sql,Mysql,Sql,我有这样一个问题: SELECT *, SUM(money_deposit + bonus_deposit) as money_deposit_total, SUM(money_withdraw + bonus_withdraw) as money_withdraw_total FROM transactions where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' group by game_

我有这样一个问题:

SELECT *, 
SUM(money_deposit + bonus_deposit) as money_deposit_total,
SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
FROM transactions 
where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' 
group by game_id;
我想做的是按日期返回结果,所以我不想只返回一行,而是希望每个日期返回一行

有什么建议吗?

您只需添加以下内容即可

group by game_DATE;
在sql查询的末尾,只需添加以下内容

group by game_DATE;
在sql查询结束时,请尝试以下操作

SELECT * FROM
   (
      SELECT game_id,created_date,
      SUM(money_deposit + bonus_deposit) as money_deposit_total,
      SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
      FROM transactions 
      where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' 
      group by game_id,game_DATE;
    ) AS T INNER JOIN transactions S ON S.game_id = T.game_id
试试这个

SELECT * FROM
   (
      SELECT game_id,created_date,
      SUM(money_deposit + bonus_deposit) as money_deposit_total,
      SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
      FROM transactions 
      where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' 
      group by game_id,game_DATE;
    ) AS T INNER JOIN transactions S ON S.game_id = T.game_id

请将group by函数用于板条箱日期列

SELECT *, 
SUM(money_deposit + bonus_deposit) as money_deposit_total,
SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
FROM transactions 
where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' 
group by game_id,created_date;

请将group by函数用于板条箱日期列

SELECT *, 
SUM(money_deposit + bonus_deposit) as money_deposit_total,
SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
FROM transactions 
where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' 
group by game_id,created_date;

如果删除*,则很容易。如果没有聚合功能,则无法显示未在group by中使用的事务字段。一个常见的技巧是,如果您知道给定日期和游戏id的所有值都相同,则使用MAX函数

SELECT created_date
       ,game_id
       ,SUM(money_deposit + bonus_deposit) as money_deposit_total
       ,SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
       ,Max( field_1) as field1
       ,Max( field_2) as field1
FROM transactions 
where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' 
group by game_id,
         created_date 
另一种选择是在select中使用子查询

SELECT created_date
       ,( Select game_name from game g where g.game_id = t.game_id) as name
       ,SUM(money_deposit + bonus_deposit) as money_deposit_total
       ,SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
[…]或之后加入

   SELECT *
      FROM ( SELECT created_date
           ,game_id
           ,SUM(money_deposit + bonus_deposit) as money_deposit_total
           ,SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
           ,Max( field_1) as field1
           ,Max( field_2) as field1
    FROM transactions 
    where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' 
    group by game_id,
             created_date ) AUX
  JOIN game g ON g.gami_id = AUX.game_id

如果删除*,则很容易。如果没有聚合功能,则无法显示未在group by中使用的事务字段。一个常见的技巧是,如果您知道给定日期和游戏id的所有值都相同,则使用MAX函数

SELECT created_date
       ,game_id
       ,SUM(money_deposit + bonus_deposit) as money_deposit_total
       ,SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
       ,Max( field_1) as field1
       ,Max( field_2) as field1
FROM transactions 
where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' 
group by game_id,
         created_date 
另一种选择是在select中使用子查询

SELECT created_date
       ,( Select game_name from game g where g.game_id = t.game_id) as name
       ,SUM(money_deposit + bonus_deposit) as money_deposit_total
       ,SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
[…]或之后加入

   SELECT *
      FROM ( SELECT created_date
           ,game_id
           ,SUM(money_deposit + bonus_deposit) as money_deposit_total
           ,SUM(money_withdraw + bonus_withdraw) as money_withdraw_total
           ,Max( field_1) as field1
           ,Max( field_2) as field1
    FROM transactions 
    where player_id = 1 and created_date between '2013-01-01' and '2014-01-05' 
    group by game_id,
             created_date ) AUX
  JOIN game g ON g.gami_id = AUX.game_id

按游戏id分组,为游戏id的每个日期创建一行?然后将日期添加到您的
分组依据中
“选择*”和“分组依据”?这是怎么回事?
按游戏id分组,为
游戏id的每个日期创建一行?然后将日期添加到您的
分组依据中
“选择*”和“分组依据”?那该怎么办?我认为它对Select*不起作用。。我认为它不会与Select*一起工作。。如果游戏中有一个以上的交易,就会产生大量的重复。但是它会和游戏桌一起工作,如果一个游戏有多个交易,它会产生大量的副本。但它将与游戏桌一起工作。