【问题标题】:Where condition not working on MySQL union 2 tables条件不适用于 MySQL union 2 表
【发布时间】:2020-03-13 10:48:39
【问题描述】:

已排序!!!

我有两张桌子,一张桌子有负责人订阅。第二个表包含每个订阅的产品用户及其详细信息。我在 mysql 中合并两个表,查询工作 100% 正常,但是当我尝试使用 where 条件过滤记录时,它返回所有记录而不过滤。

您可以在下面找到我的查询!

SELECT subscription_products.subscription_id, users.id, users.full_name,
users.company, users.job, users.birthday, users.gender,
users.nric, users.passport_number, users.phone_country_code,
users.phone_number, users.handphone_country_code, users.handphone_number,  
users.email, users.nationality, wallets.current_amount,
users.created_at, users.updated_at

FROM subscription_product_users
LEFT JOIN subscription_products
ON subscription_product_users.subscription_product_id = subscription_products.id
LEFT JOIN users
ON subscription_product_users.user_id = users.id
LEFT JOIN wallets
ON users.id = wallets.user_id

UNION

SELECT person_in_charge.subscription_id, person_in_charge.user_id,
users.full_name,
users.company, users.job, users.birthday, users.gender,
users.nric, users.passport_number, users.phone_country_code,
users.phone_number, users.handphone_country_code,
users.handphone_number, users.email, users.nationality, wallets.current_amount,
users.created_at, users.updated_at 

FROM person_in_charge
LEFT JOIN users
ON person_in_charge.user_id = users.id
LEFT JOIN wallets
ON person_in_charge.user_id = wallets.user_id

where subscription_id = '1378'

有人可以帮助我吗?

【问题讨论】:

  • 在没有看到/了解您的数据的情况下,我们只能猜测这里发生了什么。请包含样本数据以获得最佳结果。
  • where 子句仅适用于第二个选择。
  • @Ravi 答案对我有用。谢谢

标签: mysql sql sql-server mysql-workbench


【解决方案1】:

在两个查询中添加过滤器,我也使用UNION ALL 而不是UNION 以获得更好的性能。如果您的查询将返回重复项并且您想避免它,请将其替换为 UNION

您需要在这样的查询中开始使用别名。

SELECT sp.subscription_id, 
       u.id, 
       u.full_name, 
       u.company, 
       u.job, 
       u.birthday, 
       u.gender, 
       u.nric, 
       u.passport_number, 
       u.phone_country_code, 
       u.phone_number, 
       u.handphone_country_code, 
       u.handphone_number, 
       u.email, 
       u.nationality, 
       w.current_amount, 
       u.created_at, 
       u.updated_at 
FROM   subscription_product_users spu 
       INNER JOIN subscription_products sp 
               ON spu.subscription_product_id = sp.id 
       LEFT JOIN users u 
              ON spu.user_id = u.id 
       LEFT JOIN wallets w 
              ON u.id = w.user_id 
WHERE  sp.subscription_id = '1378' 
UNION ALL
SELECT pic.subscription_id, 
       pic.user_id, 
       u.full_name, 
       u.company, 
       u.job, 
       u.birthday, 
       u.gender, 
       u.nric, 
       u.passport_number, 
       u.phone_country_code, 
       u.phone_number, 
       u.handphone_country_code, 
       u.handphone_number, 
       u.email, 
       u.nationality, 
       w.current_amount, 
       u.created_at, 
       u.updated_at 
FROM   person_in_charge pic 
       LEFT JOIN users u 
              ON pic.user_id = u.id 
       LEFT JOIN wallets w 
              ON pic.user_id = w.user_id 
WHERE  pic.subscription_id = '1378' 

【讨论】:

  • @MalithPamudithaFernando 当然可以,但我只是向您展示了一种更好的方法。
【解决方案2】:

尝试包装它,它可以工作

 SELECT * FROM 

    (SELECT subscription_products.subscription_id, users.id, users.full_name,
    users.company, users.job, users.birthday, users.gender,
    users.nric, users.passport_number, users.phone_country_code,
    users.phone_number, users.handphone_country_code, users.handphone_number,  
    users.email, users.nationality, wallets.current_amount,
    users.created_at, users.updated_at

    FROM subscription_product_users
    LEFT JOIN subscription_products
    ON subscription_product_users.subscription_product_id = subscription_products.id
    LEFT JOIN users
    ON subscription_product_users.user_id = users.id
    LEFT JOIN wallets
    ON users.id = wallets.user_id

    UNION

    SELECT person_in_charge.subscription_id, person_in_charge.user_id,
    users.full_name,
    users.company, users.job, users.birthday, users.gender,
    users.nric, users.passport_number, users.phone_country_code,
    users.phone_number, users.handphone_country_code,
    users.handphone_number, users.email, users.nationality, wallets.current_amount,
    users.created_at, users.updated_at 

    FROM person_in_charge
    LEFT JOIN users
    ON person_in_charge.user_id = users.id
    LEFT JOIN wallets
    ON person_in_charge.user_id = wallets.user_id) 
as tempTable

where subscription_id = '1378'

【讨论】:

    【解决方案3】:
    select * from
    (
        (select 1 as 'a')
        union
        (select 2 as 'a')
    ) as u
    where
    u.a=2
    

    【讨论】:

    • 这并不试图解决 OP 的问题。
    猜你喜欢
    • 2015-10-06
    • 2011-12-23
    • 2011-05-27
    • 2014-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-09
    相关资源
    最近更新 更多