我有一张桌子:发票
inv_id cus_id due_amt paid total_due
1 71 300 0 300
2 71 200 0 500
3 71 NULL 125 375
4 72 50 0 50
5 72 150 0 200
Run Code Online (Sandbox Code Playgroud)
我想要结果
cus_id total_due
71 375
72 200
Run Code Online (Sandbox Code Playgroud)
这是我想total_due的unique customer或以其他方式可以说我需要latest invoice的细节unique customer。
我试过的:
SELECT cus_id, total_due FROM invoice GROUP BY cus_id ORDER BY inv_id DESC
但这并没有给出所需的结果。
请有人可以帮助我..
小智 5
试试这个查询:
SELECT `cus_id` as CustId, (SELECT `total_due` FROM invoice WHERE cus_id = CustId ORDER BY `inv_id` DESC LIMIT 1) as total_due FROM invoice GROUP BY cus_id
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
251 次 |
| 最近记录: |