如何在没有组 concat的情况下将多行加入一行?
考虑我有如下表:
表用户:
uid | name
-----------
1 | A
2 | B
表元:
uid | metaid | metaval
------------------------
1 | 1 | Jsep St.
1 | 2 | St. Oak No. 15
2 | 1 | Dt. San Joseph
2 | 2 | St. Oak No. 17
2 | 3 | OA.
表元属性:
metaid | metakey
-----------------
1 | school
2 | address
3 | dept
4 | acc
我想用他们的学校和地址选择一个用户,所以查询是
SELECT
uid, name, metaval
FROM user
INNER JOIN meta ON meta.uid = user.uid
WHERE meta.metaid = 1 OR meta.metaid = 2
它会输出
| UID | Name | MetaVal |
--------------------------------
| 1 | A | Jsep St. |
| 1 | A | St. Oak No. 15 |
我想要的结果是这样的
uid | name | school | address
-------------------------------------
1 | A | Jsep St.| St. Oak No. 15
使用组 concat,具有值“学校”和“地址”的元属性不在不同的列中,而是在一列中
我尝试加入两次并且它有效,但我认为它并不优雅
SELECT
uid, name, T1.metaval as school, T2.metaval as address
FROM user
INNER JOIN meta T1 ON T1.uid = user.uid AND T1.metaid = 1
INNER JOIN meta T2 ON T2.uid = user.uid AND T2.metaid = 2
有更好的解决方案吗?