MySQL Union、表优先级和假列
我正在尝试查询 3 个几乎不相关的表。这三者的关系只有一列(电子邮件地址字段)。我创建了一个“union all”,并从所有 3 个表中提取电子邮件,然后在最后进行分组,以确保没有重复项(我也尝试过 union)。
我的问题是一切正常,但我需要知道第一个表上的每个电子邮件地址是否为 1、0 或 NULL。我尝试拉入列,然后将 '"" AS Col1' 添加到其他表中(避免错误 1222,匹配列)。我可以这样做,但是表 1 中的数据被表 2 中的“无”覆盖,然后又被表 3 中的“无”
覆盖。查询如下:
SELECT * FROM (
SELECT email AS emails, COUNT(email) AS qty, '' AS custCountry, '' AS custID, '' AS cName, active AS newsletter
FROM newsletter
WHERE email != ''
GROUP BY email
UNION ALL
SELECT email AS emails, COUNT(email) AS qty, '' AS custCountry, '' AS custID, '' AS cName, '' AS newsletter
FROM orders
WHERE email != ''
GROUP BY email
UNION ALL
SELECT email AS emails, COUNT(email) AS qty, country AS custCountry, customerID AS custID, countries.name AS cName, '' AS newsletter
FROM customers
LEFT JOIN countries ON customers.country = countries.zoneID
WHERE email != ''
GROUP BY email
) AS newtable
GROUP BY emails
ORDER BY qty DESC
它一直有效,直到我添加“活动”列,此时它会覆盖其中的所有内容活动列什么也没有。我需要那里的第三个表,因为它提取我需要保留的位置数据。如果我将第一个表放在最后,它会给我“活动”状态,但会覆盖位置列。
我不是 MySQL 专业人士(看一下!),因此非常感谢任何帮助。
I am trying to query 3 mostly unrelated tables. The relation for all three is one column (an email address field). I have created a 'union all' and pulled in email from all 3 tables and then grouped at the end to make sure there are no duplicates (I have tried union too).
My problem is that it all works fine, but I need to know if each email address is 1, 0 or NULL on the first table. I have tried pulling in the column and then adding to '"" AS Col1' to the other tables (avoid error 1222, match up the columns). I can do this but the data from table 1 gets overwritten by 'nothing' from table2 and then again by nothing in table 3.
Here's the query:
SELECT * FROM (
SELECT email AS emails, COUNT(email) AS qty, '' AS custCountry, '' AS custID, '' AS cName, active AS newsletter
FROM newsletter
WHERE email != ''
GROUP BY email
UNION ALL
SELECT email AS emails, COUNT(email) AS qty, '' AS custCountry, '' AS custID, '' AS cName, '' AS newsletter
FROM orders
WHERE email != ''
GROUP BY email
UNION ALL
SELECT email AS emails, COUNT(email) AS qty, country AS custCountry, customerID AS custID, countries.name AS cName, '' AS newsletter
FROM customers
LEFT JOIN countries ON customers.country = countries.zoneID
WHERE email != ''
GROUP BY email
) AS newtable
GROUP BY emails
ORDER BY qty DESC
It works until I add the 'active' column, at which point it overwrites whatever was in the active column with nothing. I need the third table there as it pulls in location data that I need to retain. If I were to put the first table last it would give me 'active' but overwrite the location columns.
I'm no MySQL pro (take a look!) so any help greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您想解决什么问题?
您想要将所有 3 个表中的唯一电子邮件地址放在一起吗?
如果是,您可以使用:
select email from A
联盟
选择来自 B 的电子邮件
联盟
select email from C
您的代码片段中的分组不正确。对于
group by
查询,您只能在 select 子句中包含那些存在于 group by 子句中的列,或者是聚合函数,例如min()、max()、avg( ),count()
MySQL 不会为此抛出错误,但会在无效列中显示一些随机值。其他数据库,即 Oracle 会抛出错误。
What problem are you trying to solve?
Do you want a list of unique email address from all 3 tables put together?
If yes, you can use this:
select email from A
union
select email from B
union
select email from C
The grouping is not proper in your snippet. For a
group by
query you can have only those columns in the select clause which are either present in the group by clause or are aggregate functions such asmin(), max(), avg(),count()
MySQL wont throw an error for this but will show some random values in the invalid colu. Other databases, namely Oracle would throw an error.
好的。真的很简单。只需在选择表并集后加入您需要的数据即可。这真的很明显,而且尝试解释我的问题可能比直接解决问题更困难。不过谢谢。
Ok. Simple really. Just join the data you need after selecting the union of tables. It's really obvious and probably harder to try and explain my problem than just get on and sort it. Thanks though.