You can use HOUR
function:
SELECT hour(date_visited), count(*) as number of visits
FROM visits
GROUP BY hour(date_visited)
ORDER BY count(*) DESC
If you want to give numbers to the hours according to number of visits then you can use the analytical function (MySql 8.0 or higher) as follows:
SELECT hour(date_visited), count(*) as number of visits,
row_number() over (order by count(*) desc) as num
FROM visits
GROUP BY hour(date_visited)
ORDER BY num
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…