首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

弱弱地问个sql语句小疑点

2012-06-21 
弱弱地问个sql语句小问题update adminInfo set maxOnline (select count(*) from userInfo where isOnli

弱弱地问个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


[解决办法]
定义变量,把查询语句的值赋给变量
[解决办法]

SQL code
declare @count int=select count(*) from userInfo where isOnline = 1update adminInfo set maxOnline = @count, maxDate = GETDATE() where maxOnline <@count  and adminId = 1
[解决办法]
TRY
SQL code
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 

热点排行