下面,我的第二种情况不起作用,因为我在从子查询中引用了这个名字。这个问题有什么好的解决方案?此外,我还声明了SortBy参数…该参数仅在示例中不包括。非常感谢您的帮助! 2 m1 q0 d0 J/ g) _SELECT a.[PostID],a.[Title],a.[Date_Created] ,(SELECT COUNT(VoteID) AS VoteCount FROM [VoteTable] WHERE [PostID] = a.[PostID]) AS VoteCountFROM [PostTable] aINNER JOIN [Users] bON a.Created_UserID = b.UserIDWHERE a.Approved = 1ORDER BY CASE @SortBy WHEN 1 THEN a.[Date_Created] END DESC,CASE @SortBy WHEN 2 THEN [VoteCount] END DESC : t( w! e# O/ @0 X2 ~8 c- ]- s解决方案: # p. p8 Y' h+ @* E Y' r 代替:! G7 O7 U( b7 A0 F# F' M# Q+ k
WHEN 2 THEN [VoteCount] END DESC和:) N6 b; _$ t2 w
WHEN 2 THEN (SELECT COUNT(VoteID) AS VoteCount FROM [VoteTable] WHERE [PostID] = a.[PostID]) END DESC