2012-05-01 79 views
0

結果我有這個疑問:MySQL查詢不是我找

SELECT gamer_id,COUNT(*) AS sum, SUM(amount) as amount 
     FROM sales_list 
     WHERE rdate BETWEEN '2012-04-01' AND '2012-04-30' AND gamer_id NOT IN 
      (SELECT gamer_id FROM sales_list WHERE rdate < '2012-04-01' GROUP BY gamer_id) 
     GROUP BY gamer_id 

這個查詢打印我「2」的結果,我期待只是「1」。

我有4名玩家在4月份存款2次,我不想只計算總存款數來計算存入的玩家總數。

有什麼建議嗎?

+1

想必是在結果集的記錄只是多少?或者你不想把每個玩家都視爲一個單獨的記錄? – eggyal

+1

請提供輸入和願望輸出 –

+0

補充說:可能你不需要內部查詢謝謝 –

回答

1

我真的不明白這一點,但你只需要一個玩家存款的數量或額外存入的金額?!

這應該提供的存款數:

SELECT gamer_id, COUNT(gamer_id) AS sum, SUM(amount) as amount 
FROM sales_list 
WHERE rdate BETWEEN '2012-04-01' AND '2012-04-30' 
GROUP BY gamer_id 
HAVING COUNT(gamer_id)>0 

編輯:

SELECT DISTINCT gamer_id 
FROM sales_list 
WHERE rdate BETWEEN '2012-04-01' AND '2012-04-30' 
AND gamer_id NOT IN (SELECT gamer_id 
         FROM sales_list 
         WHERE rdate < '2012-04-01') 
GROUP BY gamer_id 
+0

類似的東西,但是,我想在查詢中的那些東西: 1.總和唯一總和玩家的 2.檢查他們是否沒有存入前(只是新玩家的) – Hanan

+0

Argl,所以你只需要在那個月新的遊戲ID,無論它們多久存放一次。這與你在第一名時所提到的不同。然後沒有COUNT(),我編輯我的帖子,並添加此。 – YvesR

+0

完美!謝謝! – Hanan

0

試試這個:

SELECT sl.gamer_id,COUNT(*) AS sum, SUM(sl.amount) as amount 
     FROM sales_list sl 
     WHERE sl.rdate BETWEEN '2012-04-01' AND '2012-04-30' AND sl.gamer_id NOT IN 
      (SELECT sl1.gamer_id FROM sales_list sl1 WHERE sl1.rdate < '2012-04-01' GROUP BY sl1.gamer_id) 
     GROUP BY sl.gamer_id