我建議刪除distinct
。
讓我們假設你的表有以下內容:
| dt | ip | views |
|----------------------------|---------|-------|
| December, 01 2015 00:00:00 | 1.1.1.1 | 100 |
| December, 01 2015 00:00:00 | 2.2.2.2 | 10 |
| December, 02 2015 00:00:00 | 3.3.3.3 | 4 |
| December, 01 2014 00:00:00 | 1.1.1.1 | 100 |
| November, 01 2015 00:00:00 | 1.1.1.1 | 1 |
| November, 01 2015 00:00:00 | 2.2.2.2 | 2 |
查詢:
SELECT
@rownum := @rownum +1 `index`,
COUNT(ip), SUM(views) , dt
FROM ipAll, (SELECT @rownum :=0) r
WHERE dt LIKE '%-12-%'
GROUP BY dt
ORDER BY dt ASC
LIMIT 31;
將導致:
| index | COUNT(ip) | SUM(views) | dt |
|-------|-------------|--------------|----------------------------|
| 3 | 1 | 100 | December, 01 2014 00:00:00 |
| 1 | 2 | 110 | December, 01 2015 00:00:00 |
| 2 | 1 | 4 | December, 02 2015 00:00:00 |
得到的另一種方法相似(不完全相同)結果是:
select @rownum := @rownum +1 `index`, main.*
from (SELECT @rownum :=0) r, (
SELECT COUNT(ip), SUM(views) , dt
FROM ipAll
WHERE dt LIKE '%-12-%'
GROUP BY dt
LIMIT 31
) main
ORDER BY dt ASC
個
其結果將是:
| index | COUNT(ip) | SUM(views) | dt |
|-------|-------------|--------------|----------------------------|
| 1 | 1 | 100 | December, 01 2014 00:00:00 |
| 2 | 2 | 110 | December, 01 2015 00:00:00 |
| 3 | 1 | 4 | December, 02 2015 00:00:00 |
SQLFiddle例如:http://sqlfiddle.com/#!9/beb98/3
你是什麼意思結合起來?你能給出這兩個查詢結果的例子嗎?你想如何合併這些結果? 在MySQL中,有一個UNION語句,它將合併多個查詢的結果。但是,結果需要具有相同的列。 –