2

我有一个这样的查询:

select *, coalesce((select top 1 filename from logos where uid=id),'nologo.jpg') from users

但它的接缝并不支持它。我怎样才能做到这一点?

4

1 回答 1

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
于 2013-08-05T10:51:44.657 回答