问题
I am trying to UNION
two tables in a subquery, but apparently this is not allowed? A simplified version of my query is:
SELECT *
FROM user, organization
WHERE user.id NOT IN (
(SELECT distinct user_id FROM web_request)
UNION
(SELECT distinct user_id from user_milestone)
)
AND user.organization_id = organization.id
My question is this: What is the best way to get around this shortcoming of MySQL?
回答1:
SELECT u.*, o.*
FROM `user` u
INNER JOIN organization o
ON u.organization_id = o.id
WHERE u.id NOT IN(
SELECT distinct user_id FROM web_request
UNION
SELECT distinct user_id from user_milestone
)
回答2:
Try this
SELECT *
FROM USER u
JOIN organization o
ON u.organization_id = o.id
WHERE u.uid NOT IN SELECT * (
(
SELECT DISTINCT user_id
FROM web_request )
union
(
SELECT DISTINCT user_id
FROM user_milestone) ) AS t
来源:https://stackoverflow.com/questions/37598964/union-two-tables-in-a-subquery