我尝试执行SQL查询,该查询将返回具有默认值的自定义列。
这是实际查询:
SELECT A.ID, A.NAME
FROM dbo.A
WHERE (A.ID NOT IN (SELECT B.ID FROM dbo.B)) AND (A.ID = 5)
但我想按如下方式返回结果 -
ID | Name | Address
===============================
1 | abc | Not available
2 | xyz | Not available
请注意,Address
列不在数据库表中。
有什么办法吗?
答案 0 :(得分:1)
SELECT A.ID, A.NAME, 'Not available' as Address
FROM dbo.A
WHERE (A.ID NOT IN (SELECT B.ID FROM dbo.B)) AND (A.ID=5)
答案 1 :(得分:0)
将Not available
添加为列,并使用LEFT JOIN
代替IN
子句。
试试这个:
SELECT A.ID, A.NAME, 'Not available' AS Address
FROM dbo.A
LEFT OUTER JOIN dbo.B ON A.ID = B.ID
WHERE B.ID IS NULL AND A.ID=5