2012-03-28 111 views
1

選擇行我有這個疑問來計算一週中最常見的日期和時間:匹配計算

SELECT 
    DATE_FORMAT(DATE, '%a') AS wd, 
    HOUR(DATE) AS hr, 
    COUNT(*) AS cnt 
FROM 
users 
GROUP BY 
    wd, hr 
ORDER BY 
    cnt DESC 

現在,我要選擇符合wd = "Fri" AND hr = "9"

我的所有行的ID的已經嘗試添加WHERE子句,但由於我不能在別名上使用WHERE,所以我無法做到這一點。

+0

的數據和詢問你到現在爲止。 – 2012-03-28 11:26:17

回答

1
SELECT ID 
FROM users 
WHERE DATE_FORMAT(DATE, '%a') = 'Fri' AND HOUR(DATE) = 9 
0
SELECT 
    DATE_FORMAT(DATE, '%a') AS wd, 
    HOUR(DATE) AS hr, 
    COUNT(*) AS cnt 
FROM users 
WHERE DATE_FORMAT(DATE, '%a') = 'Fri' 
AND HOUR(DATE) = 9 
GROUP BY wd, hr 
ORDER BY cnt DESC 

你可以這樣做,也是使用別名的選擇,但性能會一樣好,因爲這樣一來過濾正在發生的表的份額前聚集