是否可以将两个表的UNION
的结果与第三个表联系起来?像这样:
(SELECT DISTINCT `Fund_ID`, `Fund_Name` FROM `admin`
UNION
SELECT `Fund_ID`,`Fund_Name` FROM `admin_custom` WHERE `admin_custom`.`user_id` = 361) a
LEFT JOIN `qt1`
ON `qt1`.`Fund ID` = a.`Fund_ID`
但此代码不起作用。我可以在JOIN
之前在SELECT
个查询中移动UNION
,但宁愿尝试使用JOIN
结果UNION
。
我该如何解决这个问题?
答案 0 :(得分:7)
是的,有可能。但是,您的代码不正确,因为您缺少SELECT
语句本身,因为您的第一个选择变为行集(运行时创建的表)。即您必须指定要获取的SELECT
运算符和字段。最简单的情况:
SELECT
a.*
FROM
(SELECT DISTINCT `Fund_ID`, `Fund_Name` FROM `admin`
UNION
SELECT `Fund_ID`,`Fund_Name` FROM `admin_custom` WHERE `admin_custom`.`user_id` = 361) AS a
LEFT JOIN `qt1`
ON `qt1`.`Fund ID` = a.`Fund_ID`
答案 1 :(得分:2)
SELECT * FROM
(SELECT DISTINCT `Fund_ID`, `Fund_Name` FROM `admin`
UNION
SELECT `Fund_ID`,`Fund_Name` FROM `admin_custom` WHERE `admin_custom`.`user_id` = 361) a
LEFT JOIN `qt1`
ON `qt1`.`Fund ID` = a.`Fund_ID`;