【问题标题】:How to select latest date record and show other columns in mysql?如何选择最新的日期记录并显示mysql中的其他列?
【发布时间】:2017-06-06 05:23:46
【问题描述】:

我正在使用 XAMPP MYSQL

我有表,Date 列是字符串

  Date    |  Customer   |   Location   | Cash | Balance |
2015/6/23    LBalance      California    5000    3000
2015/6/25    LBalance      New York      6000    4500
2015/6/25    InHoss Dept.  South Beach   15000   1000
2015/6/20    InHoss Dept.  South Beach   15000   1000
2015/6/17    InHoss Dept.  South Beach   15000   1000
2015/6/22    LBalance      California    5000    4000
2015/6/22    LBalance      New York      6000    6000
2015/6/17    InHoss Dept.  North Beach   18000   1000

如何选择查询每个客户不同地点的最新日期记录?

预期结果:

Date      |  Customer   |    Location   | Cash | Balance |
2015/6/23    LBalance      California    5000    3000
2015/6/25    LBalance      New York      6000    4500
2015/6/25    InHoss Dept.  South Beach   15000   1000
2015/6/17    InHoss Dept.  North Beach   18000   1000

这是我去过的最近的一次:SELECT Customer, Location, Cash, Balance, max(Date) AS “As Of” FROM mytable WHERE Customer = 'LBalance' GROUP BY Customer, Location。虽然它显示的是客户的最近日期,但另一列只是显示他们的第一个结果。

【问题讨论】:

  • 提示:GROUP BYMAX
  • @TimBiegeleisen 我猜这是作业
  • 请在实际示例中显示您想要什么,预期结果是什么
  • 我只想查看每个用户结果的最新日期。
  • @MonPadi 以下答案没有回答您的问题?

标签: php sql


【解决方案1】:

从 xampp 中选择 (*) 其中 date = (select max(date) from xampp)

【讨论】:

    【解决方案2】:

    要获取最新记录,您使用 max() 函数,对于您使用的每个客户和位置分组 检查以下内容:

    select max(date) , CUSTOMER, LOCATION from table GROUP BY CUSTOMER , LOCATION
    

    编辑。如果日期是 varchar(逻辑错误),你可以在下面做。

     select Max(cast(date as Date)) , CUSTOMER, LOCATION from table GROUP BY CUSTOMER , LOCATION
    

    【讨论】:

      【解决方案3】:

      因为,您想要一个日期到字符串的转换。但不确定是否需要这样做,因为 max(date) 可能在没有转换的情况下仍然有效。

      select max(STR_TO_DATE(Date, '%Y/%m/%d')),Customer, location, cash,balance from YourTable group by Customer, Location
      

      Happy Coding.. 请确认您需要什么。变得难以跟踪编辑。

      【讨论】:

        【解决方案4】:

        也许你可以试试这个

        select a.*
        from yourtable a JOIN
        ( select Customer,Location,Max(Date) as Mdate
           from yourtable
           group by Customer,Location) b
        on a.Customer=b.Customer and a.Location=b.Location and a.Date=b.Mdate
        

        【讨论】:

          【解决方案5】:

          试试看

          with cte (date, Customer, Location, Cash , Balance) as
          (
          select cast('2015/6/23' as datetime) date, 'LBalance' Customer, 'California' Location, 5000 Cash , 3000 Balance
          union all
          select cast('2015/6/25' as datetime) date, 'LBalance' Customer, 'New York' Location, 6000 Cash , 4500 Balance
          union all
          select cast('2015/6/25' as datetime) date, 'InHoss Dept.' Customer, 'South Beach' Location, 15000 Cash , 1000 Balance
          union all
          select cast('2015/6/20' as datetime) date, 'InHoss Dept.' Customer, 'South Beach' Location, 15000 Cash , 1000 Balance
          union all
          select cast('2015/6/17' as datetime) date, 'InHoss Dept.' Customer, 'South Beach' Location, 15000 Cash , 1000 Balance
          union all
          select cast('2015/6/22' as datetime) date, 'LBalance' Customer, 'California' Location, 5000 Cash , 4000 Balance
          union all
          select cast('2015/6/22' as datetime) date, 'LBalance' Customer, 'New York' Location, 6000 Cash , 6000 Balance
          union all
          select cast('2015/6/27' as datetime) date, 'InHoss Dept.' Customer, 'North Beach' Location, 18000 Cash , 1000 Balance
          )
          
          select m.Date, m.Customer, m.Location, m.Cash, m.Balance 
             from cte m
             where m.Date = (select max(mm.Date) 
                               from cte mm 
                              where mm.customer=m.customer
                                and mm.location = m.location )
          

          我已经检查了我的示例,一切正常,请确保您使用的是 mysql 示例

          【讨论】:

          • OP 想要最新的record,我解释为它想要整个记录。
          • 它只是重复并显示所有日期和记录。我只想显示上述预期结果。
          • 重试我已添加位置
          • 还是一样的先生
          • 这是我去过的最接近的地方:SELECT Customer, Location, Cash, Balance, max(Date) AS "MostRecentServiceMonth" FROM mytable WHERE Customer = 'LBalance' 分组 Customer, Location。虽然它显示的是客户的最近日期,但另一列只是显示他们的第一个结果。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-11
          • 1970-01-01
          • 2013-02-04
          • 2017-03-15
          • 2020-08-22
          • 2023-01-13
          相关资源
          最近更新 更多