排序!

我有2个表,一个表有负责人认购。第二个表有product用户为每个订阅和他们的详细资料。我union无论是在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'

有人可以帮助我吗?

分析解答

尝试把它包起来,它的工作

 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'