dongquexi1990 2015-08-09 11:27
浏览 28

按组ID查看mysql顺序的最新存储值

your query execute only this is not correct see another example below

pid  customer id  invoice id     name  due amount make payment

 1      1001           086    sasi      36       sasi      10 
 5     1003            084    ram       100      ram       50
 7     1002            083    koushil   80       am         50

query is

SELECT * FROM print_reciept pr INNER JOIN ( SELECT invoice_id, MIN(pid) AS min_pid FROM print_reciept WHERE due_amount != 0 GROUP BY invoice_id ) t ON pr.invoice_id = t.invoice_id AND pr.pid = t.min_pid WHERE due_amount != 0;

see this again example

pid  customer id  invoice id  name  due amount make_pay paid_amount

1      1001           086    sasi      36       sasi      10   
2      1001           086    sasi      26       ram        5
3      1001           086    sasi      21       ravi      10 
4      1001           086    sasi      11       sasi      10     
5     1003            084    ram       100      ram       50
6     1003            084    raghu     50       sasi      10 
7     1002            083    koushil   80       am         50
8      1002           083     koushil  30        am        20 

this is my result but i am already pay due 6 amount in same id use invoice id 086

i need result

pid  customer id  invoice id     name  due amount make payment

 4      1001           086       sasi         11       sasi      10
 6     1003            084      raghu         50       sasi      10 
 8      1002           083     koushil        30        am       20 
  • 写回答

2条回答 默认 最新

  • doushang4274 2015-08-11 01:50
    关注

    I'm not sure I understood you correctly.

    If you want to get the last value of invoice_id = 086:

    SELECT pid, cust_id, cust_name, date, invoice_id, invoice_date, 
      invoice_amount, paid_amount, paymentmade, makeapayment, due_amount, status
    FROM print_reciept
    WHERE invoice_id = 086
      AND due_amount != 0
    ORDER BY pid DESC
    LIMIT 1;
    

    If you want to get the last value for each invoice_id:

    SELECT *
    FROM print_reciept pr
        INNER JOIN
        (
            SELECT invoice_id, MIN(pid) AS min_pid
            FROM print_reciept
            WHERE due_amount != 0
            GROUP BY invoice_id
        ) t ON pr.invoice_id = t.invoice_id AND pr.pid = t.min_pid
    WHERE due_amount != 0;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败