0
我有這個疑問:如何在我的MySQL使用GROUP_CONCAT
select IF(user_name IS NULL or user_name= '', distinct_id, user_name)
as user_info, event, count(event) as event_count
from mixpanel
where
(mixpanel.event = 'app_open' or
mixpanel.event = 'post_created' or
mixpanel.event = 'create_comment' or
mixpanel.event = 'class_opened' or
mixpanel.event = 'post_read')
AND
class_id = 'AKSJSDKSD'
group by event, user_name, distinct_id
order by user_name desc
,它給了我下面的結果:
+-----------+------------+--------+
| user_info | event | event_count |
+-----------+------------+--------+
| Ben Cho | up | 12 |
| Lee Mar | up | 21 |
| Lee Mar | side | 12 |
| Lee Mar | down | 16 |
| Al Gov | up | 14 |
| Al Gov | down | 13 |
| Al Gov | side | 13 |
+-----------+------------+--------+
我需要的是聚集像這樣每個用戶數據:
+-----------+------------+--------+
| user_info | up | down | side
+-----------+------------+--------+
| Ben Cho | 12 | 0 | 0 |
| Lee Mar | 21 | 16 | 12 |
| Al Gov | 14 | 13 | 13 |
+-----------+------------+--------+
我該如何在mySql上實現這個功能?
謝謝!
這不是用'group_concat'實現的。據我所知,你正在做一次樞軸操作。我不知道在MySQL中是否有自動的方式。你也可以閱讀手冊(我必須爲了找出答案)。 –
我會的。感謝您的當場快速回復@JonathanLeffler –