求教:我的SQL语句错在哪?

海里猿人 2014-07-03 08:06:43

update table as a set see =1 where exists (select 1 from table as b where a.username =b.username and convert(varchar(10),a.日期,121) =convert(varchar(10),b.日期,121) and b.type=1) and a.type=2

我想UPDATE的是在同一张表中,有如果在同一天,同一用户,已经有type=1的,就把type=2的SEE更为为1.
可是我却发现没有TYPE=1的,也被改掉了。
...全文
235 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_17298075 2014-07-06
  • 打赏
  • 举报
回复
如果表中的字段或者表名用的是SQL系统的关键字,可以用"[]"括起来,建议表名和字段不要用系统的关键字..
Neo_whl 2014-07-04
  • 打赏
  • 举报
回复
楼上基本说道了所有可能性,全角空格和关键字用[]括起来,语句目测没问题
jmx123456789 2014-07-04
  • 打赏
  • 举报
回复
没有这么直接写过。 update的时候都是from TB 来直接连接表。
无涯大者 2014-07-03
  • 打赏
  • 举报
回复
如果表中的字段或者表名用的是SQL系统的关键字,可以用"[]"括起来,建议表名和字段不要用系统的关键字..
以学习为目的 2014-07-03
  • 打赏
  • 举报
回复
update table set see =1 where exists (select 1 from table b where username =b.username and convert(varchar(10),日期,121) =convert(varchar(10),b.日期,121) and b.type=1) and type=2
xiaoxiangqing 2014-07-03
  • 打赏
  • 举报
回复
自己可以测试下,语句是没有问题的
唐诗三百首 2014-07-03
  • 打赏
  • 举报
回复
try this,

update a  
 set a.see=1 
 from [table] a
 where exists 
 (select 1 
  from [table] b 
  where a.username=b.username 
  and convert(varchar,a.日期,121)=convert(varchar,b.日期,121)
  and b.type=1) and a.type=2
發糞塗牆 2014-07-03
  • 打赏
  • 举报
回复
试试这个:
UPDATE a
SET a.see=1
FROM [table] AS a INNER JOIN [table] AS b ON a.username = b.username
                        AND CONVERT(VARCHAR(10), a.日期, 121) = CONVERT(VARCHAR(10), b.日期, 121)
WHERE b.[type]=1 AND a.[type]=2
卖水果的net 版主 2014-07-03
  • 打赏
  • 举报
回复


--  把 table as a 中的 as a 去掉,再把子查询中的 a.col 都改成 table.col 
update table 
set see =1 where exists (select 1 from table as b where table.username =b.username and convert(varchar(10),table.日期,121) =convert(varchar(10),b.日期,121)  and b.type=1) 

-- 另外 ,如果非要使用别名形式,可以使用下列格式
update a  
set a.see = 1 
from table a , table b 
where a.username =b.username 
and convert(varchar(10),a.日期,121) =convert(varchar(10),b.日期,121
and b.type=1) 
and a.type=2

-- PS 好象你的语句中有 全角空格。
發糞塗牆 2014-07-03
  • 打赏
  • 举报
回复
先保证语法无错
UPDATE  a
SET     see = 1
FROM    [table] AS a
WHERE   EXISTS ( SELECT 1
                 FROM   [table] AS b
                 WHERE  a.username = b.username
                        AND CONVERT(VARCHAR(10), a.日期, 121) = CONVERT(VARCHAR(10), b.日期, 121)
                        AND b.type = 1 )
        AND a.type = 2

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧