MySQL透视行和列

时间:2015-11-15 09:41:15

标签: mysql

我有一个包含一些用户数据的表,第二个包含用户属性数据。这些属性总是2,我知道他们的名字。我需要一个查询来检索单个结构中的所有内容。但是,我无法改变数据库架构。

这是我的数据库的简化版本:

USER

+-------------------+--------------+------+-----+---------+-------+
| Field             | Type         | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| username          | varchar(64)  | NO   | PRI | NULL    |       |
| name              | varchar(100) | YES  |     | NULL    |       |
+-------------------+--------------+------+-----+---------+-------+

USER_PROPERTIES

+-----------+--------------+------+-----+---------+-------+
| Field     | Type         | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+-------+
| username  | varchar(64)  | NO   | PRI | NULL    |       |
| propName  | varchar(100) | NO   | PRI | NULL    |       |
| propValue | text         | NO   |     | NULL    |       |
+-----------+--------------+------+-----+---------+-------+

所以,举例来说,这个数据:

USER

username         name
1                User1
2                User2

USER_PROPERTIES

username         propName         propValue
1                status           "At work"
1                picture          "pict1.jpg"
2                status           "Busy"
2                picture          "pict2.jpg"

我需要以下结果:

username         name             STATUS          PICTURE
1                User1            "At work"       "pict1.jpg"
2                User2            "Busy"          "pict2.jpg"

我在互联网上做了一些研究,显然这是通过PIVOT实现的,但MySQL不包含这个功能。通过这里的答案:MySQL pivot table,我可以设法得到这个:

select ou.username, 
    case when (oup.propName='status') then oup.propValue end as 'STATUS',
    case when (oup.propName='picture') then oup.propValue end as 'PICTURE'
from User ou, User_Properties oup
where ou.username = oup.username;

username         name             STATUS          PICTURE
1                User1            "At work"       null
1                User1            null            "pict1.jpg"
2                User2            "Busy"          null
2                User2            null            "pict2.jpg"

结果分为两个不同的部分。如果我按用户名对结果进行分组,我会将PICTURE数据始终显示为空:

select ou.username, 
    case when (oup.propName='status') then oup.propValue end as 'STATUS',
    case when (oup.propName='picture') then oup.propValue end as 'PICTURE'
from User ou, User_Properties oup
where ou.username = oup.username
group by oup.username;

username         name             STATUS          PICTURE
1                User1            "At work"       null
2                User2            "Busy"          null

我错过了什么?感谢。

编辑:https://stackoverflow.com/users/1529673/strawberry给出了解决方案:

select ou.username, 
    MAX(case when (oup.propName='status') then oup.propValue end) as 'STATUS',
    MAX(case when (oup.propName='picture') then oup.propValue end) as 'PICTURE'
from User ou, User_Properties oup
where ou.username = oup.username;

1 个答案:

答案 0 :(得分:0)

https://stackoverflow.com/users/1529673/strawberry给出了解决方案:

select ou.username, 
    MAX(case when oup.propName='status' then oup.propValue end) as 'STATUS',
    MAX(case when oup.propName='picture' then oup.propValue end) as 'PICTURE'
from User ou
Join User_Properties oup
On ou.username = oup.username;