我有以下3个表格:
custom_data table:
+----+----------------------+
| id | name |
+----+----------------------+
| 13 | ACCOUNT NAME |
| 14 | ACCOUNT NO |
| 15 | ID NUMBER |
+----+----------------------+
custom_data_values表:
+----------------+-----------------+-----------+
| custom_data_id | value | member_id |
+----------------+-----------------+-----------+
| 13 | Ivy Jane Mosendi| 33 |
| 14 | 2199030092 | 33 |
| 15 | 25917781 | 33 |
+----------------+-----------------+-----------+
会员表:
+----+----------------------------+
| id | name |
+----+----------------------------+
| 30 | John Doe |
| 31 | Willy Ong |
| 32 | James Reid |
| 33 | Ivy Jane Mosendi |
+----+----------------------------+
我想要以下结果:
+----+------------------+------------------+-------------+-------------+
| id | name | ACCOUNT NAME | ACCOUNT NO | ID NUMBER |
+----+------------------+------------------+-------------+-------------+
| 33 | Ivy Jane Mosendi | Ivy Jane Mosendi | 2199030092 | 25917781 |
+----+------------------+------------------+-------------+-------------+
如何使用mysql查询实现此结果?
答案 0 :(得分:2)
我终于破解了它。谢谢大家
SELECT m.name
, MAX(CASE WHEN cf.name = 'ACCOUNT NAME' Then cfv.string_value END) As 'ACCOUNT NAME'
, MAX(CASE WHEN cf.name = 'ACCOUNT NO' Then cfv.string_value END) As 'ACCOUNT NO'
, MAX(CASE WHEN cf.name = 'ID NUMBER' Then cfv.string_value END) As 'ID NUMBER'
FROM custom_field_values cfv
INNER JOIN custom_fields cf
ON cf.id = cfv.field_id
INNER JOIN members m
ON m.id = cfv.member_id
WHERE m.id = 33