简体   繁体   中英

MySQL Pivoting rows and columns

I have a table with some user data, and a second one with user properties data. The properties are always going to be 2, and I know their name. I need a query to retrieve everything in a single structure. I cannot alter, however, the database schema.

This is the simplified version of my database:

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    |       |
+-----------+--------------+------+-----+---------+-------+

So, having for example, this data:

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"

I would need the following result:

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

I did some research in Internet and apparently this is achieved with PIVOT, but MySQL does not contain this functionality. By following the answers here: MySQL pivot table , I could manage to get this:

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"

The results are in two different lines. If I group the results by username, I get the PICTURE data always as null:

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

What am I missing? Thanks.

EDIT: https://stackoverflow.com/users/1529673/strawberry gave the solution:

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;

https://stackoverflow.com/users/1529673/strawberry gave the solution:

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM