sql问题,两个表比较

来源:百度知道 编辑:UC知道 时间:2024/07/02 05:04:57
有两个表a和b,都有字段title,且b的title记录在a中都存在。现在想做的是:
如果b中的title在a中有,则修改a的字段has为1.

语句如下:
update
set b.has=1 from a,b where a.title=b.title

但是运行结果是一条都没有更新。把=换成like也不行。

请教高手,如何才能做到?
谢谢各位。
a,b两个表中的字段都是nvarchar类型。
当我把它们改成char类型时,上述语句就可以正常运行了。
只是我不知道原因。
----------------
谢谢lazy67,
a表中的title尾部确实有空格,但我现在还去不掉。
我用了ltrim和rtrim都不行,如下:
select ltrim(rtrim(title)) from a
结果还是有空格。

另外,字段类型是char还是nvarchar并不影响,我刚又试了,又可以了,真是乱。
=================================
谢谢各位,问题解决了。
是因为title里面有个回车符,但用select显示出来是个空格,所以rtrim去不掉这个空格。
可惜只能给一个人分,我只能对其他人说声谢谢。

update a set a.has=1 from a, b where a.title=b.title
如果还不行, 注意 title字段是否有空格或其它不可见字符, 格式化即可.

update
set a.has=1 from a where a.title in (select b.title from b )

update a set a.has=1 from a, b where upper(ltrim(rtrim(a.title)))=upper(ltrim(rtrim(b.title)))

update a
set a.has=1
from a,b
where a.title=b.title

楼主没有指定更新哪个表!完毕!

你varchar的长度是不是给的太短了。。设置长点

update a
set a.has=1
from a,b
where rtrim(a.title) = rtrim(b.title)



update a
set a.has=1
from a,b
where charindex(a.title,rtrim(b.title)) > 0

第二种慎用