2012-10-24 145 views
1

我需要統計所有FirstExtracted爲特定的日期,我需要統計所有LastExtracted爲同一日期。所以,今天,我需要所有FirstExtractedLastExtracted等於2012-10-24如何統計所有結果和計數具體在同一個查詢 - MySql

這是我到目前爲止,但它沒有帶來LastExtracted。它輸出LastExtracted的數量相同FirstExtracted

(SELECT LastExtracted,FirstExtracted, 
COUNT(FirstExtracted) AS FirstCount, 
COUNT(LastExtracted) AS LastCount, 
DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate, 
DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate 
FROM results 
WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d')) 
UNION ALL 
(SELECT LastExtracted,FirstExtracted, 
COUNT(FirstExtracted) AS FirstCount, 
COUNT(LastExtracted) AS LastCount, 
DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate, 
DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate 
FROM results 
WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') GROUP BY Firstdate) 
ORDER BY Firstdate DESC 
LIMIT 20 

也許我應該使用內部聯接?

更新: 所以使用您的查詢,我做了一些更改,使它現在爲我做了別的事情。如果你看一下這個頁面我把查詢了

semesterold.com/code2.html

我想指望由藝術家所有標題和組。這將是一個數組。那麼我希望子查詢通過匹配藝術家的searchtype進行計數。所以如果db有akon,rihanna,chris brown。我希望它能夠統計出每個藝術家有多少個標題,比如說akon有100個。然後我想要顯示標題數量,然後統計這100個標題中有多少是Google,bing,特定於akon的站點等。

+0

什麼是表格定義? –

+0

表格定義是什麼意思? –

回答

0

我會使用union得到兩個結果(FirstExtracted和LastExtracted)分開,然後使用子查詢合併。

SELECT 
    sub.date, sum(sub.FirstCount) AS FirstCount, 
    sum(sub.LastCount) AS LastCount 
    FROM (
     SELECT 
      DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS date, 
      COUNT(FirstExtracted) AS FirstCount, 0 AS LastCount 
     FROM results 
     GROUP BY date 
     UNION ALL 
     SELECT 
      DATE_FORMAT(`LastExtracted`,'%Y-%m-%d'), 
      0, COUNT(LastExtracted) 
      FROM results 
      GROUP BY date 
    ) AS sub 
    GROUP BY sub.date 

說你今天萃取今日最近提取第一時間和292 220項。其中用於其它日期的條目,這將給你:

+------------+------------+-----------+ 
| date  | FirstCount | LastCount | 
+------------+------------+-----------+ 
| 2012-10-24 |  220 |  292 | 
+------------+------------+-----------+ 

更新單獨的UNION會給你下面的結果。注意零佔位符。

+------------+------------+-----------+ 
| date  | FirstCount | LastCount | 
+------------+------------+-----------+ 
| 2012-10-24 |  220 |   0 | 
+------------+------------+-----------+ 
| 2012-10-24 |   0 |  292 | 
+------------+------------+-----------+ 
+0

這工作完美!你能解釋一下你是如何做到的以及零是什麼?我對理解它很感興趣。 –

+0

「UNION」中的第一個查詢僅選擇給定日期的「FirstExtracted」的計數。第二個抓取給定日期的'LastExtracted'計數。兩者都使用零作爲其他佔位符。然後,外部查詢按日期將兩個合併爲一行,因此220 + 0和0 + 292會給出上述結果。 – qsheets

+0

但在第二部分中,您選擇了DATE_FORMAT(xx),0,COUNT(LastExtracted)爲什麼會有零?還有,當你把sub.date這意味着它從聯合查詢調用日期,所以你可以選擇它們運行之前在聯合查詢中的變量?我認爲sql按順序進行。如果你調用sub.date和sum(sub.FirstCount),它們還沒有被定義,直到FROM之後。這非常有趣。 –

0

我建議在查詢中添加DISTINCT

(SELECT LastExtracted,FirstExtracted, 
COUNT(DISTINCT FirstExtracted) AS FirstCount, 
COUNT(DISTINCT LastExtracted) AS LastCount, 
DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate, 
DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate 
FROM results 
WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d')) 
UNION ALL 
(SELECT LastExtracted,FirstExtracted, 
COUNT(DISTINCT FirstExtracted) AS FirstCount, 
COUNT(DISTINCT LastExtracted) AS LastCount, 
DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') AS Lastdate, 
DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') AS Firstdate 
FROM results 
WHERE DATE_FORMAT(`FirstExtracted`,'%Y-%m-%d') = DATE_FORMAT(`LastExtracted`,'%Y-%m-%d') GROUP BY Firstdate) 
ORDER BY Firstdate DESC 
LIMIT 20 
+0

這將如何幫助?只是好奇,因爲它似乎希望將同一日期第一次或最後一次提取的所有條目加起來,同時保持第一次和最後一次分開。使用'DISTINCT'使得它看起來像只計算不同日期而不是同一日期的日期。 – qsheets

相關問題