0

我目前有一个我无法弄清楚的 SQL Server 问题。

错误是:

为变量赋值的 SELECT 语句不得与数据检索操作结合使用

SQL:

DECLARE @IdUser INT 

Select 
    @IdUser = Id, 
    Username, 
    (Select Count(*) From GagsLikes where Userid = @IdUser And Good = 1) as GagLikes, 
    (Select Count(*) From GagsViews where UserID = @IdUser), 
    (Select Count(*) From Gags Where UserID = @IdUser) as GagViews 
From 
    Users  
Order by 
    GagLikes, GagViews

提前致谢!

4

2 回答 2

2

您可能不会在执行此操作时使用该变量,请尝试以下操作:

Select 
    U.Id, 
    U.Username, 
    (Select Count(*) From GagsLikes where Userid = U.Id And Good = 1) as GagLikes, 
    (Select Count(*) From GagsViews where UserID = U.Id), 
    (Select Count(*) From Gags Where UserID = U.Id) as GagViews 
From 
    Users AS U
Order by 
    GagLikes, GagViews
于 2013-07-07T20:18:19.310 回答
1

您的表可能会返回多于一行。确保您的表只返回一行,否则它无法将多个 id 存储到一个 int 变量中。

于 2013-07-07T20:25:50.763 回答