1

我有这个 mssql 查询

SELECT TOP 50 [id], [title], [url], [icon], linkstats.visits 
FROM [websites] LEFT OUTER JOIN [linkstats] ON websites.id=linkstats.lid AND linkstats.code=@country 
WHERE (([country] = @country OR [country]= 'all') AND 
      ([hot] = @hot)) 
ORDER BY linkstats.visits DESC

我想按空值排序到最后,如果都是空值,那么按 ID 排序怎么做?!

4

1 回答 1

0

采用CASE

SELECT TOP 50 [id], [title], [url], [icon], linkstats.visits 
FROM [websites] LEFT OUTER JOIN [linkstats] ON websites.id=linkstats.lid AND linkstats.code=@country 
WHERE (([country] = @country OR [country]= 'all') AND 
      ([hot] = @hot)) 
ORDER BY CASE WHEN linkstats.lid IS NULL THEN 1 ELSE 0 END,
         linkstats.visits DESC
于 2013-01-14T07:40:12.840 回答