子查询中的两个表

子查询中的两个表

本文介绍了UNION 子查询中的两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在子查询中UNION 两个表,但显然这是不允许的?我的查询的简化版本是:

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

我的问题是:解决 MySQL 的这个缺点的最佳方法是什么?

My question is this: What is the best way to get around this shortcoming of MySQL?

推荐答案

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
)

这篇关于UNION 子查询中的两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:53