Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL查询以获取Group BY中的SUM和LAST记录值_Mysql_Group By_Subquery - Fatal编程技术网

MYSQL查询以获取Group BY中的SUM和LAST记录值

MYSQL查询以获取Group BY中的SUM和LAST记录值,mysql,group-by,subquery,Mysql,Group By,Subquery,我有两个表1是account,另一个是account\u spend\u history,因为ash account是父/主表和shin a子表。并且与accountaccount表id有关系。account表id是ash中的外键,作为account\u id。请参见图 现在我需要得到account.id,total spend,它是具有相同account\u id的amount\u spend和last spend的总和,是针对account\u id插入ash表中的最后一条记录,即与MAXa

我有两个表1是account,另一个是account\u spend\u history,因为ash account是父/主表和shin a子表。并且与accountaccount表id有关系。account表id是ash中的外键,作为account\u id。请参见图

现在我需要得到account.id,total spend,它是具有相同account\u id的amount\u spend和last spend的总和,是针对account\u id插入ash表中的最后一条记录,即与MAXash.id对应的amount\u spend值,即

id | spend_total | last_spend --------------------------------- 1 | 30 | 18 2 | 280 | 120 3 | 20 | 20 我得到account.id和ash.amount花费的总和,但我还需要最后一次花费。怎么弄到的

谢谢。

这里有一个使用相关子查询的选项:


您可以使用in子句和子查询

SELECT a.id, SUM(ash.amount_spend) AS spend_total, x.last_spend 
FROM accounts as a
INNER JOIN account_spend_history AS ash ON ash.account_id = a.id
INNER JOIN  ( 
  select account_id, last_spend, start_date 
  from account_spend_history
  where (start_date, acoount_id) in  ( 
      select  max(start_date), account_id 
      from account_spend_history 
      group by account_id 
    ) 
) x on x.id = a.id
GROUP BY a.id
您也可以在查询中获取MAXash.id,然后使用它连接回account\u Expense\u history表:


嗨@Giorgos Betsos,谢谢你帮助我。我写了一个查询,但有一个错误。1054-字段列表中的未知列“发送金额”。从选择a.id、SUMash.amount\u spend AS spend\u total、amount\u send AS last\u spend中选择t1.id、spend\u total、amount\u send AS last\u spend,MAXash.id作为ash\u id来自accounts作为内部连接帐户\u spend\u history作为ash上的ash.account\u id=a.id按a.id分组作为t1内部连接帐户\u spend\u history作为ash上的ash.id=t1.ash\u idThank@sgedes,它对我有效,我得到了所需的输出。
SELECT a.id, SUM(ash.amount_spend) AS spend_total,
       (SELECT amount_spend 
        FROM account_spend_history as ash2 
        WHERE ash2.account_id = a.id 
        ORDER BY ash2.id DESC LIMIT 1) as last_spend
FROM accounts as a
    INNER JOIN account_spend_history AS ash ON ash.account_id = a.id
GROUP BY a.id
SELECT a.id, SUM(ash.amount_spend) AS spend_total, x.last_spend 
FROM accounts as a
INNER JOIN account_spend_history AS ash ON ash.account_id = a.id
INNER JOIN  ( 
  select account_id, last_spend, start_date 
  from account_spend_history
  where (start_date, acoount_id) in  ( 
      select  max(start_date), account_id 
      from account_spend_history 
      group by account_id 
    ) 
) x on x.id = a.id
GROUP BY a.id
SELECT id, spend_total, amount_send as last_spend
FROM (
  SELECT a.id, SUM(ash.amount_spend) AS spend_total, MAX(ash.id) AS ash_id
  FROM accounts as a
  INNER JOIN account_spend_history AS ash ON ash.account_id = a.id
  GROUP BY a.id) AS t1
INNER JOIN account_spend_history AS ash ON ash.id = t1.ash_id