嗨,我正在使用MSSQL 2017,并尝试弄清楚此代码的工作原理
DECLARE @customer TABLE (
credithold bit,
id int primary key,
deliverycityID int,
postalcityID int
)
DECLARE @city TABLE (
cityid int primary key,
pop bigint
)
INSERT INTO @city VALUES (1,2666),(2,7889),(3,28917)
INSERT INTO @customer VALUES(1,1,2,1),(0,2,3,2),(1,3,1,3),(0,4,2,3)
SELECT c1.id, pop, IIF(credithold = 0, deliverycityID, postalcityID) as cityid
FROM @customer c1
INNER JOIN @city AS A ON A.cityid = cityid
代码给出的结果如下(https://i.stack.imgur.com/qxs2G.png)但我认为它应该像这样返回(https://i.stack.imgur.com/x3B19.png)https://i.stack.imgur.com/x3B19.png有什么建议吗?谢谢!
对不起,该图片,由于信誉不足,我无法直接发布该图片。
通过此操作进行正确的连接只需执行以下操作:
SELECT c1.id, pop, IIF(credithold = 0, deliverycityID, postalcityID) as cityid
FROM @customer c1
INNER JOIN @city AS A ON A.cityid = IIF(credithold = 0, deliverycityID, postalcityID)