UNION two tables in a subquery

情到浓时终转凉″ 提交于 2019-12-13 08:25:42

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!