与 不同的是ORDER BY
,WHERE
联合中的子句仅影响联合的那一部分(SELECT
它附加到的单个查询),而不影响整个联合。
将您的联合放在子查询中并过滤结果:
SELECT *
FROM
(
SELECT *
FROM DCHDR AS H
LEFT JOIN DCDTL AS T ON H.DC_No = T.DC_Nos
UNION
SELECT *
FROM DCHDR AS H
RIGHT JOIN DCDTL AS T ON H.DC_No = T.DC_Nos
) H
WHERE H.DC_date BETWEEN '2013-03-02' AND '2013-03-11'
ORDER BY `Product_Desc` ASC
LIMIT 510, 30
或为每个部分复制您的条件:
SELECT *
FROM DCHDR AS H
LEFT JOIN DCDTL AS T ON H.DC_No = T.DC_Nos
WHERE H.DC_date BETWEEN '2013-03-02' AND '2013-03-11'
UNION
SELECT *
FROM DCHDR AS H
RIGHT JOIN DCDTL AS T ON H.DC_No = T.DC_Nos
WHERE H.DC_date BETWEEN '2013-03-02' AND '2013-03-11'
ORDER BY `Product_Desc` ASC
LIMIT 510 , 30
看起来您正在尝试模拟完全连接。我是否可以建议使用以下查询,它应该更快(并且实际上做你想要的,而不是过滤联合的第二部分):
SELECT *
FROM DCHDR AS H
LEFT JOIN DCDTL AS T ON H.DC_No = T.DC_Nos
WHERE H.DC_date BETWEEN '2013-03-02' AND '2013-03-11'
UNION ALL
SELECT *
FROM DCHDR AS H
RIGHT JOIN DCDTL AS T ON H.DC_No = T.DC_Nos
WHERE H.DC_No IS NULL
ORDER BY `Product_Desc` ASC
LIMIT 510 , 30
DCDTL
请注意,如果您故意尝试使用 过滤掉重复的行,则UNION
可以将其更改UNION ALL
为UNION
,但我怀疑实际上并非如此。