弱弱地问个sql语句小问题
update adminInfo set maxOnline = (select count(*) from userInfo where isOnline = 1), maxDate = GETDATE() where maxOnline < (select count(*) from userInfo where isOnline = 1) and adminId = 1
这个语句有两次(select count(*) from userInfo where isOnline = 1) 怎样写只需要查询一次呢?
[解决办法]
在外面定义一个变量不就好了吗
DECLARE @COUNT INT
select @COUNT=count(*) from userInfo where isOnline = 1
update adminInfo set maxOnline = @COUNT, maxDate = GETDATE() where maxOnline < @COUNT and adminId = 1
[解决办法]
定义变量,把查询语句的值赋给变量
[解决办法]
declare @count int=select count(*) from userInfo where isOnline = 1update adminInfo set maxOnline = @count, maxDate = GETDATE() where maxOnline <@count and adminId = 1
[解决办法]
TRY
update adminInfo set maxOnline = b.total , maxDate = GETDATE() from (select count(*) total from userInfo where isOnline = 1) b where where maxOnline < b.total and adminId = 1