Php 如何在mysql中选择最新日期记录并显示其他列?

Php 如何在mysql中选择最新日期记录并显示其他列?,php,sql,Php,Sql,我正在使用XAMPP MYSQL 我有一个表,日期列是字符串 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

我正在使用XAMPP MYSQL

我有一个表,日期列是字符串

  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

这是我见过的最接近的一个:从mytable中选择Customer、Location、Cash、Balance、maxDate,其中Customer='LBalance'按客户、地点分组。虽然它显示的是客户最近的日期,但另一列只是显示他们的第一个结果。

select*from xampp 其中date=从xampp中选择maxdate

试试

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示例来获取您使用max函数的最新记录,以及您分组使用的每个客户和位置 检查波纹管:

select max(date) , CUSTOMER, LOCATION from table GROUP BY CUSTOMER , LOCATION
编辑。如果日期在逻辑上是错误的,你可以在下面做

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

因为,您需要日期到字符串的转换。但不确定是否需要这样做,因为maxdate在没有转换的情况下仍然可以工作

select max(STR_TO_DATE(Date, '%Y/%m/%d')),Customer, location, cash,balance from YourTable group by Customer, Location
快乐编码。。请确认您需要什么。很难跟踪编辑内容。

也许您可以试试这个

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

提示:使用分组MAX@TimBiegeleisen我想这是一个家庭作业。请在真实的例子中说明你想要什么,预期的结果是什么。我只想看到每个用户结果的最新日期。@MonPadi下面的答案没有回答你的问题?OP想要最新的记录,我的意思是它需要整个记录,它只是重复和显示所有的日期和记录。我只想显示上面的预期结果。请重试我已添加了Location完全相同的Sir这是我最近访问过的:从mytable中选择Customer、Location、Cash、Balance、maxDate作为MostRecentServiceMonth,其中Customer='LBalance'按客户、地点分组。虽然它显示的是客户最近的日期,但另一列只是显示他们的第一个结果。