gpt4 book ai didi

MySQL - 使用 UNION ALL 获取错误的表字段,但结果是正确的

转载 作者:行者123 更新时间:2023-11-29 22:00:59 25 4
gpt4 key购买 nike

我正在尝试通过与提交的电子邮件匹配来从两个表中查找记录。现在结果变得正确,但表字段错误。

品牌表:

brand_id    business_email              password
-------- -------------- --------
2 mack@gmail.com ********

Instagrammer 表:

instagrammer_id     email                   password
--------------- ----- --------
1 manan@gmail.com *********

我有这个 mysql 联合查询:

SELECT brand_id, business_email, password
FROM brand
WHERE business_email = 'manan@gmail.com'
AND STATUS = '1'
UNION ALL
SELECT instagrammer_id, email, password
FROM instagrammer
WHERE email = 'manan@gmail.com'
AND STATUS = '1'
LIMIT 0 , 30

并得到这个结果:

brand_id    business_email              password
-------- -------------- --------
1 manan@gmail.com ********

正确的输出是:

instagrammer_id     email                   password
--------------- ----- --------
1 manan@gmail.com *********

结果是正确的,但表字段是错误的:(任何想法如何解决这个问题并获得正确的字段。

最佳答案

union/union all 查询的列名称来自第一个子查询。您可以使用别名来定义它们:

SELECT brand_id as instagrammer_id, business_email as email, password
FROM brand
WHERE business_email = 'manan@gmail.com'
AND STATUS = '1'
UNION ALL
SELECT instagrammer_id, email, password
FROM instagrammer
WHERE email = 'manan@gmail.com'
AND STATUS = '1'
LIMIT 0 , 30

关于MySQL - 使用 UNION ALL 获取错误的表字段,但结果是正确的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32651028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com