【发布时间】:2013-11-08 11:33:29
【问题描述】:
我正在尝试将同一个表中的 3 个电子邮件列连接到 1 个大电子邮件列中,我可以使用以下 SQL 来完成:
SELECT email
FROM (
SELECT email
FROM accounts
UNION
SELECT email2
FROM accounts
UNION
SELECT email3
FROM accounts
)accounts WHERE email LIKE '%@%'
但我也希望能够仅将同一表(帐户组)中的不同列 = 的行返回到特定值。例如,我认为可行的方法:
SELECT email, accountgroup
FROM (
SELECT email, accountgroup
FROM accounts
UNION
SELECT email2, accountgroup
FROM accounts
UNION
SELECT email3, accountgroup
FROM accounts
)accounts
WHERE email LIKE '%@%'
AND accountgroup = 'Vyrav'
第二条语句没有按预期工作,只返回 1 行。我认为 UNION 正在合并 accountgroup 列值相同的行(这将是所有行)。
如果我修改第一条语句使最后一行像这样:
)accounts WHERE email LIKE '%@%' AND accountgroup='Vyrav'
它说'where子句'中有一个'未知列'accountgroup'
谁能建议我在这里做错了什么,或者我应该怎么做?
在此先感谢,非常感谢任何帮助。
【问题讨论】:
标签: mysql sql merge union where-clause