这涉及一些猜测 - 是否admin
仅与quant1
? 而admin_custom
只是quant1_cust
?那么这应该减少这两个字段的不同列表所需的工作量:
SELECT `Fund_ID`, `Fund_Name`
FROM `admin`
INNER JOIN `quant1` ON `admin`.`Fund_ID` = `quant1`.`Fund_ID`
AND `quant1`.`VaR 95`>-0.028
UNION
SELECT `Fund_ID`, `Fund_Name`
FROM `admin_custom`
INNER JOIN `quant1_cust` ON `admin_custom`.`Fund_ID` = `quant1_cust`.`Fund_ID`
AND `admin_custom`.`user_id` = `quant1_cust`.`user_id`
AND `quant1_cust`.`VaR 95`>-0.028
WHERE `admin_custom`.`user_id`=361
;
查看您现有的查询结构,我建议您不要做几件事。使用 UNION 和 SELECT DISTINCT 没有意义。不要将 select * 与 UNION 或 UNION ALL 一起使用 - 要具体且仅涉及您真正需要的字段。并且您现有的 where 子句确实会抑制左连接返回的任何空值 - 所以不要使用左连接。
SELECT DISTINCT --<< effort for distinctiveness here
...
FROM (
SELECT * --<< too many fields
...
UNION --<< effort for distinctiveness here
SELECT * --<< too many fields
...
)
LEFT JOIN (
SELECT * --<< too many fields
...
UNION --<< effort for distinctiveness here
SELECT * --<< too many fields
...
) quant1
WHERE quant1 ...
编辑:替代 - 抱歉,这样可能更容易遵循:
SELECT `Fund_ID`, `Fund_Name`
FROM `admin`
INNER JOIN `quant1` ON `admin`.`Fund_ID` = `quant1`.`Fund_ID`
WHERE `quant1`.`VaR 95`>-0.028
UNION
SELECT `Fund_ID`, `Fund_Name`
FROM `admin_custom`
INNER JOIN `quant1_cust` ON `admin_custom`.`Fund_ID` = `quant1_cust`.`Fund_ID`
AND `admin_custom`.`user_id` = `quant1_cust`.`user_id`
WHERE `admin_custom`.`user_id`=361
AND `quant1_cust`.`VaR 95`>-0.028
;