sql从id中选择多个值

sql从id中选择多个值

这是我的桌子的样子:

fid  | section  | baseData
59   |    58    |  somedata
59   |    58    |  somedata
60   |    58    |  somedata

我需要这样的数据:
所有者、姓名、电子邮件
所有值都在表baseData中,但所有者是fid=59,名称是fid 70,电子邮件是fid 71
所以我想得到:
 owner       |  name       |  email
 owner_name  |  user_name  | user_email
 owner_name2 |  user_name2 | user_email2

我试着这样做:
SELECT fid,
(CASE
WHEN (fid = 59 AND section = 58)
THEN baseData
END) AS owner
FROM sobipro_field_data
WHERE section = 58 GROUP BY fid

但只有零分
我想得到的是:
When fid = 59 -> select baseData as owner
When fid = 70 -> select baseData as name
When fid = 71 -> select baseData as email

最佳答案

对于一个部分:

SELECT
    max(CASE WHEN (fid = 59) THEN baseData ELSE null END) AS owner,
    max(CASE WHEN (fid = 70) THEN baseData ELSE null END) AS name,
    max(CASE WHEN (fid = 71) THEN baseData ELSE null END) AS email
FROM sobipro_field_data
WHERE section = 58;

对于每个部分:
SELECT section,
    max(CASE WHEN (fid = 59) THEN baseData ELSE null END) AS owner,
    max(CASE WHEN (fid = 70) THEN baseData ELSE null END) AS name,
    max(CASE WHEN (fid = 71) THEN baseData ELSE null END) AS email
FROM sobipro_field_data
group by section;

关于mysql - sql从id中选择多个值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27187209/

10-09 01:37