我不明白为什么右联接未按预期返回结果。
明确说明条件的结果仅返回以下内容:
R.REIN_SRT = 'A' AND R.REIN_STA = '9'
但是,它返回表中的所有内容,就像没有where条件一样:
+----------+-------------+----------+----------+----------+
| INVE_KEY | INVE_NME | INVE_KEY | REIN_SRT | REIN_STA |
+----------+-------------+----------+----------+----------+
| NULL | NULL | 1 | S | 1 |
| 1 | CHAIR BLACK | 1 | A | 9 |
| NULL | NULL | 1 | B | 2 |
| NULL | NULL | 1 | S | 1 |
| NULL | NULL | 1 | S | 1 |
| NULL | NULL | 1 | S | 1 |
| NULL | NULL | 1 | S | 1 |
| NULL | NULL | 1 | S | 1 |
| NULL | NULL | 1 | S | 1 |
+----------+-------------+----------+----------+----------+
为什么条件无法按预期运行?
这是整个示例:
DECLARE @JEINVE TABLE
(
INVE_KEY INT,
INVE_NME VARCHAR(100)
)
DECLARE @JEREIN TABLE
(
INVE_KEY INT,
REIN_SRT CHAR(1),
REIN_STA CHAR(1)
)
INSERT INTO @JEINVE(INVE_KEY, INVE_NME)
VALUES(1, 'CHAIR BLACK'),(2, 'WHITE TABLE'),(3, 'BLACK CANDLE')
INSERT INTO @JEREIN(INVE_KEY,REIN_SRT,REIN_STA)
VALUES(1,'S','1'),(1,'A','9'),(1,'B','2'),(1,'S','1'),(1,'S','1'),(1,'S','1'),(1,'S','1'),(1,'S','1'),(1,'S','1')
SELECT *
FROM @JEINVE INVE RIGHT JOIN @JEREIN R
ON INVE.INVE_KEY = R.INVE_KEY
AND R.REIN_SRT = 'A' AND R.REIN_STA = '9'
答案 0 :(得分:3)
修改您的查询:
SELECT *
FROM @JEINVE INVE
RIGHT JOIN @JEREIN R
ON INVE.INVE_KEY = R.INVE_KEY
WHERE R.REIN_SRT = 'A' AND R.REIN_STA = '9'
更改查询:
现有
AND R.REIN_SRT = 'A' AND R.REIN_STA = '9'
新功能:
WHERE R.REIN_SRT = 'A' AND R.REIN_STA = '9'
说明:
当您将条件置于“ ON CLAUSE”时
但是当您将条件放在“ WHERE子句”中