分组查询最新记录

时间:2015-12-30 08:21:48

标签: php

我只需要查询一个表,并使用group by查询仅显示最新记录 我想按手机号码分组

我的专栏名称如下:

Reservation_idClient_nameEmail_addressMobile_numberVehical_type

所以我想将此列分组并显示最新记录

我尝试了这个查询:

SELECT `Reservation_id`,`Client_name`,`Email_address`,`Mobile_number`
FROM `car_reservation` GROUP BY `Mobile_number` ASC       
ORDER BY `Reservation_id` ASC;

3 个答案:

答案 0 :(得分:1)

这是最终的QUERY SELECT Reservation_id,Client_name,Email_address,Mobile_number,Pickup_date_time,Pickup_address,Drop_off_address,Distance,Total_price,Vehical_type FROM car_reservation WHERE Reservation_id IN ( SELECT MAX(Reservation_id) FROM car_reservation GROUP BY Mobile_number ORDER BY Reservation_id desc ) ORDER BY Reservation_id DESC

答案 1 :(得分:0)

SELECT Reservation_id,Client_name,Email_address,Mobile_number FROM car_reservation WHERE Mobile_number in (SELECT Mobile_number FROM car_reservation ORDER BY Reservation_id ASC LIMIT 0,1); 

我希望这是你所期待的。

答案 2 :(得分:-1)

试试这个..

 $sql = "SELECT `Reservation_id`,`Client_name`,`Email_address`,`Mobile_number`
FROM car_reservation GROUP BY Mobile_number ASC ORDER BY Reservation_id ASC LIMIT 1";