我有这样的查询:
select *, coalesce((select top 1 filename from logos where uid=id),'nologo.jpg') from users
但是它的联合并不支持它。我怎么能这样做?
答案 0 :(得分:1)
试试这个 -
SELECT
u.*
, ISNULL(filename, 'nologo.jpg')
FROM dbo.users u
OUTER APPLY
(
SELECT TOP 1 [filename]
FROM dbo.logos
WHERE uid = id
) t
或者这个 -
SELECT
u.*
, COALESCE([filename], 'nologo.jpg')
FROM dbo.users u
LEFT JOIN (
SELECT id, [filename] = MAX([filename])
FROM dbo.logos
GROUP BY id
) t ON u.uid = t.id
您的查询也有效 -
SELECT
*
, COALESCE((
SELECT TOP 1 [filename]
FROM dbo.logos
WHERE uid = id
)
, 'nologo.jpg')
FROM dbo.users