简体   繁体   English

mysql:分组并获取最新记录

[英]mysql: group by and get latest record

I have query like this: 我有这样的查询:

SELECT cus_id, ROUND(SUM(credit_in)-SUM(credit_out), 2) as balance, date_added
FROM `customer_wallet`
GROUP BY cus_id

It will get the customer's balance (unique customers). 它将获得客户的余额(唯一的客户)。 I want to modify this if balance is in minus then it'll come and I want latest date ( date_added ). 如果余额为负,我想修改此值,然后我想要最新日期( date_added )。

I tried by doing this: 我尝试这样做:

SELECT cus_id, ROUND(SUM(credit_in)-SUM(credit_out), 2) as balance, date_added
FROM `customer_wallet`
GROUP BY cus_id
ORDER BY date_added

But, it's giving sorted records after getting all result. 但是,它会在得到所有结果后给出排序的记录。 I want latest record for individual customer. 我想要个人客户的最新记录。

Let me know if you need more info or schema. 让我知道您是否需要更多信息或架构。 Thanks. 谢谢。

I can check records with minus balance in php as well. 我也可以在php查看余额为负的记录。 But, it'll be great if I can do it in query itself. 但是,如果我可以在查询本身中做到这一点,那就太好了。

You need to use an aggregate function to select the most recent date -- I believe the default is for mysql to just pick a random value for that column. 您需要使用聚合函数来选择最近的日期-我相信默认值是mysql只为该列选择一个随机值。

SELECT cus_id, 
       ROUND(SUM(credit_in)-SUM(credit_out), 2) as balance, 
       max(date_added) as most_recent
FROM `customer_wallet`
Having SUM(credit_in) < SUM(credit_out)
GROUP BY cus_id

MYSQL by default doesn't force you to put all columns which are not included in aggregated function in the Group By clause. 默认情况下,MYSQL不会强制您将未包含在聚合函数中的所有列都放在Group By子句中。 This can return strange results. 这会返回奇怪的结果。

Try the following query. 请尝试以下查询。

  SELECT cus_id, 
         ROUND(SUM(credit_in)-SUM(credit_out), 2) as balance, 
         max(date_added) latest_transaction_date
    FROM `customer_wallet`
GROUP BY cus_id
  HAVING ROUND(SUM(credit_in)-SUM(credit_out), 2) < 0

If you want to read more about the Group By in MySQL you can check this blog post: Debunking GROUP BY myths . 如果您想了解有关MySQL中Group By依据的更多信息,可以查看此博客文章: Debunking GROUP BY myths It's quite old, but still interesting if you are new to MySQL. 它已经很老了,但是如果您是MySQL的新手,它仍然很有趣。

It is unclear from your question, but I assuming you want the negative balances for individuals and the date of their last transaction. 从您的问题尚不清楚,但是我假设您想要个人的负余额及其最后交易的日期。

SELECT cus_id, ROUND(SUM(credit_in)-SUM(credit_out), 2) as balance, max(date_added) as last_date
FROM `customer_wallet`
HAVING ROUND(SUM(credit_in)-SUM(credit_out) < 0
GROUP BY cus_id
ORDER BY last_date

If you don't want just the negative balances then use this: 如果您不只是想要负余额,请使用以下命令:

SELECT cus_id, ROUND(SUM(credit_in)-SUM(credit_out), 2) as balance, max(date_added) as last_date
FROM `customer_wallet`
GROUP BY cus_id
ORDER BY last_date

So I am making some assumptions here (I also saw the sql tag, so I am hoping an answer in sql will be helpful): 所以我在这里做一些假设(我也看到了sql标记,所以我希望sql中的答案会有所帮助):

You have a customer table: cust_id is the primary key, and a given customer has only one row dedicated to them 您有一个客户表:cust_id是主键,并且给定的客户只有一行专用于他们

You have a wallet table with credits. 您有一个带积分的钱包表。 cust_id is a foreign key, and any customer can have multiple rows in this table. cust_id是一个外键,任何客户在此表中都可以有多行。 WalletId is the primary key in this table, and the most recent transactions have the highest number WalletId是此表中的主键,并且最近的事务具有最高数量

It will be tough to do this while returning just cust_id. 仅返回cust_id时很难做到这一点。 If you aren't averse to returning 1 extra column this can be done easily (the walletid). 如果您不反对返回额外的1列,则可以轻松完成此操作(walletid)。 You just need to add import date to the group clause, and utilize the max function against walletid. 您只需要将导入日期添加到group子句中,并针对walletid使用max函数。

SELECT cus_id, MAX(walletid), ROUND(SUM(credit_in)-SUM(credit_out), 2) as balance, date_added
FROM customer_wallet
GROUP BY IssuerId, date_added
ORDER BY date_added

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM