2010-11-10 55 views
3

好吧我有MYSQL查詢代碼設置將這些信息從我的數據庫中提取出來。我現在想要做的是按代碼計算,但按名稱分組。MySQL查詢幫助計數請

$sql = "SELECT m.* 
      FROM (SELECT DISTINCT Code 
        FROM Transaction) md 
      JOIN Transaction m ON m.ID_Transaction = (SELECT ID_Transaction 
                 FROM Transaction mi 
                WHERE mi.Code = md.Code 
                 AND Date_Time=CURdate() 
                 AND Time_Stamp!='' 
                ORDER BY m.Name DESC, mi.Code DESC, mi.Date_Time DESC, mi.ID_Transaction DESC 
                LIMIT 1)"; 

用我的php代碼,這是我從這個查詢中得到的。

rick, 002455, 2010-11-10, 08:30 AM 
rick, 003819, 2010-11-10, 08:45 AM 
amber, 003572, 2010-11-10, 08:45 AM 
eric, 001479, 2010-11-10, 10:30 AM 
jerry, 001012, 2010-11-10, 09:45 AM 

裏克是名
000000爲代碼

我想利用這個信息,像這樣爲每名指望它。

rick = 2 
amber = 1 
eric = 1 
jerry = 1 

我不知道我該怎麼做?我可以使用查詢中的信息進行另一個查詢來計算此信息。

回答

2
SELECT m.name, count(*) FROM (
SELECT DISTINCT Code 
FROM Transaction 
) md JOIN Transaction m ON 
m.ID_Transaction = (SELECT ID_Transaction FROM Transaction mi WHERE mi.Code = md.Code AND Date_Time=CURdate() AND Time_Stamp!='' ORDER BY m.Name DESC, mi.Code DESC, mi.Date_Time DESC, mi.ID_Transaction DESC LIMIT 1) 
group by m.name