我在以下URL上创建了我的SQL Fiddle,用于测试和构建我的查询:
http://sqlfiddle.com/#!2/5415d/3
考虑我的表的以下结构:
TABLE1 +-------------------------+ | Username | Event ID | +-------------------------+ | Jhon | ABC | | Nick | ABC | | Hen | ABC | | Jhon | DEF | | Jhon | ABC | +-------------------------+
我试图编写的查询是当用户想要查看与用户Jhon
相关的eventsID时,您可以看到上表中有各种类似的jhon,而有些则不是这样查询应该按照Count (Event_ID)
的降序顺序返回类似于以下输出的内容:
+-----------------------------------------------+ | Username | Event ID | Count (Event_ID) | +-----------------------------------------------+ | Jhon | ABC | 3 | | Jhon | DEF | 1 | +-----------------------------------------------+
请告诉我如何撰写此类查询。
谢谢,
答案 0 :(得分:1)
select min(username) as username,
eventid,
count(eventid) as event_count
from table1
where username = 'Jhon'
group by eventid
order by event_count desc