2015-12-30 171 views
-1

我需要組通過查詢只有一張桌子,並使用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; 
+0

嘿,夥計,「GROUP BY ... ASC」? – Raptor

+0

你的問題不清楚。你可以給我們一個例子,給定的記錄應該輸出什麼? – Yogesh

+0

請參閱下面的答案。 ;) – Yogesh

回答

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

試試這個..

$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"; 
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); 

我希望這是你期待什麼。