2017-06-06 100 views
-3

我使用的XAMPP MYSQL如何選擇最新的日期記錄並在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 

這是最接近我去過:AS 「爲」 FROM mytable的WHERE客戶= 'LBalance' 選擇客戶,地點,現金,平衡,最大(日) GROUP BY客戶,地點。儘管它顯示了客戶的最近日期,但另一列僅顯示他們的第一個結果。

+1

提示:'GROUP BY'用'MAX' –

+0

@TimBiegeleisen我猜它的一門功課 – Moudiz

+0

請出示你想在真實的例子是什麼,會有什麼預期的結果 – Vecchiasignora

回答

0

選擇(*)從XAMPP 其中date =(選擇最大(從XAMPP日期))

+0

我編輯了我的帖子。 –

0

嘗試

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) 

我在我的例子everething已經檢查是確定的,請確保你正在使用MySQL實例

+1

該OP想要最新的「記錄」,我解釋它意味着它想要整個記錄。 –

+0

謝謝你,我已經改變了答案,試試吧 – Vecchiasignora

+0

不工作先生。 –

0

爲每個客戶和位置,您使用組由 檢查波紋管,讓你使用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 
+0

我編輯了我的問題先生。謝謝:) –

0

因爲,你想要一個日期字符串轉換。不確定這是否需要,因爲,max(日期)可能仍然工作沒有轉換。

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

快樂編碼..請確認您需要什麼。變得難以跟蹤編輯。

+0

我編輯了我的問題先生。謝謝 :) –

0

也許你可以試試這個

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 
相關問題