2011-03-09 140 views
0

我有一張收集客戶付款的表。我想從前3名客戶那裏得到每個客戶的付款。同一張表中的MySQL子查詢

下面的查詢獲得每個客戶每天每小時的總付款。下面

SELECT 
    hour(received) as hr, 
    login, 
    count(*) as total 
FROM 
    payment 
WHERE 
    received >= date("2011-02-24") 
AND 
    received <date("2011-02-25") 
AND 
    PAYMENT_BATCH_FILE_ID is null 
GROUP BY 
    hr, 
    client_id 
ORDER BY 
    hr, 
    total DESC 

查詢獲得前3名的客戶端爲每一個客戶在一個小時內

SELECT 
    hour(received) as hr, 
    login, 
    count(*) as total 
FROM 
    payment 
WHERE 
    received >=date("2011-02-24") 
AND 
    received < date("2011-02-25") 
AND 
    PAYMENT_BATCH_FILE_ID is null 
AND 
    hour(received)="3" 
GROUP BY 
    hr, 
    client_id 
ORDER BY 
    hr, 
    total DESC 
LIMIT 3 

如何將兩者結合起來的查詢獲得前3名的客戶,使每個小時支付?

謝謝。

+1

有點失去了你在找什麼...你想要前3名的客戶嗎?或者每天每小時(72個結果)還是?還有點在「一小時內讓每位客戶獲得前三名客戶」中輸掉? – CarpeNoctumDC 2011-03-09 08:44:25

+0

我想在一天中的每個小時內獲得3位客戶的信息 – muhdazmilug 2011-03-10 00:36:04

回答