【问题标题】:Mysql latest record for distinct column不同列的Mysql最新记录
【发布时间】:2017-09-20 12:21:05
【问题描述】:

我有一张桌子:发票

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

我想要结果

cus_id   total_due
71       375  
72       200

那是我想要unique customertotal_due 或者说我需要unique customerlatest invoice 详细信息。

我尝试了什么:

SELECT cus_id, total_due FROM invoice GROUP BY cus_id ORDER BY inv_id DESC

但这并没有给出所需的结果。

请有人可以帮助我..

【问题讨论】:

  • 试试这个: where inv_id=(select max(inv_id) from table_a as a inner join table_a as b on A.cus_id = B.cus_id)。查找每个 cus_id 的最大 inv_id 的 total_due 是一个自联接。

标签: mysql group-by


【解决方案1】:

试试这个查询:

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

【讨论】:

    【解决方案2】:

    创建一个子查询来获取客户最近的total_due

    SELECT cus_id, (select total_due from invoice where inv_id=max(a.inv_id)) as total_due FROM invoice a GROUP BY cus_id ORDER BY inv_id DESC
    

    Demo here

    【讨论】:

      【解决方案3】:

      试试这个示例查询

      SELECT i1.cus_id,i1.total_due FROM invoice as i1
      LEFT JOIN invoice AS i2 ON i1.cus_id=i2.cus_id AND i1.inv_id<i2.inv_id
      WHERE i2.inv_id IS NULL  
      

      【讨论】:

        【解决方案4】:

        只需根据cus_id 的组和inv_id 的降序给出一个行号。然后选择行号为 1 的行。

        查询

        select t1.cus_id, t1.total_due from (
            select cus_id, total_due, (
                case cus_id when @a 
                then @b := @b + 1 
                else @b := 1 and @a := cus_id end 
            ) as rn 
            from your_table_name t, 
            (select @b := 0, @a := '') r 
            order by cus_id, inv_id desc 
        ) t1 
        where t1.rn = 1
        order by t1.cus_id;
        

        Find a demo here

        【讨论】:

          【解决方案5】:

          查询:

          SELECT cus_id, total_due FROM invoice GROUP BY cus_id ORDER BY inv_id DESC 
          

          由于SELECT 子句中的total_due 列,SQL 无效。

          GROUP BY 的查询允许包含在 SELECT 子句中:

          1. GROUP BY 子句中也存在的表达式;
          2. 使用aggregate functions 的表达式(又名“GROUP BY”函数);
          3. 在功能上依赖于 GROUP BY 子句中存在的列的列。

          表达式total_due 不是上述两种情况。

          在 5.7.5 版本之前,MySQL 曾经接受此类无效查询。但是,服务器对return indeterminate values for the invalid expressions 是免费的。从 5.7.5 版本开始,MySQL 拒绝此类查询(其他 RDBMS 很久以前就拒绝它们......)。

          为什么这样的查询无效?

          因为GROUP BY 查询从表中返回行。它创建它返回的行。对于它放入结果集中的每一行,它使用表中的一组行。对于 GROUP BY 子句中出现的表达式,组中的所有行都具有相同的值,但它们在出现在 SELECT 子句中的其他表达式中可能具有不同的值。

          这个特定问题的正确解决方案是什么?

          我之前在 StackOverflow 上多次回答过这个问题。查看this answerthis answerthis answerthis answer 并将您从中学到的知识应用到您的查询中。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2019-07-13
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-03-15
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多