我试图将包含多列的2个表链接在一起。例如,我有一个名为' wp_users '我想用作主表,另一张名为' CD2015_Occurrences '
我想关联' wp_users ' [ EMAIL ]至 CD2015_Occurrences ' [ EMAIL ]这很简单,但我想添加内部联接。
所以它看起来像这样
电子邮件| Q1 | Q2 | Q3 | Q4 |
email1 | 2 | 4 | 5 | 6
email2 | 4 | 5 | 6 | 0
等...
表格内容' CD2015_Occurrences '是电子邮件,季度,总计
我需要以某种方式通过电子邮件将它们链接到wp_users,并且每个季度都有4个单独的列。
我可能关闭,但这是我正在尝试做的事情:
Select a.user_email
FROM wp_users AS a
INNER JOIN a.email
ON CD2015_Occurrences.email as b (SELECT quarter AS Q1 FROM CD2015_Occurrences where email = a.email and quarter = 'q1')
INNER JOIN a.email
ON CD2015_Occurrences.email as b (SELECT quarter AS Q2 FROM CD2015_Occurrences where email = a.email and quarter = 'q2')
INNER JOIN a.email
ON CD2015_Occurrences.email as b (SELECT quarter AS Q3 FROM CD2015_Occurrences where email = a.email and quarter = 'q3')
INNER JOIN a.email
ON CD2015_Occurrences.email as b (SELECT quarter AS Q4 FROM CD2015_Occurrences where email = a.email and quarter = 'q4')
任何想法,如果这是可能的以及如何? 非常感谢 最大
答案 0 :(得分:1)
SELECT users.email,
SUM(IF(occurences.quarter='q1',occurences.total,0)) Q1,
SUM(IF(occurences.quarter='q2',occurences.total,0)) Q2,
SUM(IF(occurences.quarter='q3',occurences.total,0)) Q3,
SUM(IF(occurences.quarter='q4',occurences.total,0)) Q4
FROM wp_users users
LEFT JOIN CD2015_Occurrences occurrences
ON users.email = occurrences.email
GROUP BY users.email