MySQL列有多个值用作下一列的标识符。表结构:
id (key), occurrence_id, name, value
name
列值对应于values
列。如何在一个视图中显示此信息?
目前,它看起来像这样:
occurrence_id name value
1576 Attempts 1
1576 ClientIP "94.xxx.xxx.xxx"
1576 UserAgent ""
1576 CurrentUserID 0<
我想制作一个如下所示的视图:
occurrence_id Attempts Client IP CurrentUserID
1576 1 "94.xxx.xxx.xxx" 0
2009 30 "68.111.xxx.xxx" 0
答案 0 :(得分:0)
一种方法是使用条件聚合:
select occurrence_id,
max(case when name = 'Attempts' then value end) as Attempts,
max(case when name = 'ClientIP' then value end) as ClientIP,
max(case when name = 'UserAgent' then value end) as UserAgent,
max(case when name = 'CurrentUserID' then value end) as CurrentUserID
from table t
group by occurrence_id;