SQL,判断一个表中的字段是否存在于另一个表中,如何写

SQL,判断一个表中的字段是否存在于另一个表中,如何写
没学过SQL,请详细说明

楼主说的是更新吧,楼上说的是sql
server的语法,不知道楼主是什么数据库,如果是oracle的话
建议这么写:
update
a
set
col=(select
col
from
b
where
a.id=b.id)
exists(select
1
from
b
where
a.id=b.id

注意:两个表的id
一定要一一对应,不让会报错:查询单个值返回多条记录。
温馨提示:内容为网友见解,仅供参考
第1个回答  2010-05-19
假设你说的字段名称相同就算存在的话
select name from syscolumns where id=(select id from sysobjects where name='表名A')
and name in (select name from syscolumns where id=(select id from sysobjects where name='表名B'))
如果有,查询显示相同字段名
如果没有,查询显示无
第2个回答  推荐于2016-02-04
你是说的字段名还是字段的具体值?
字段名:
if (NOT exists ( select * from dbo.syscolumns where name = '字段名' and id in
(select id from dbo.sysobjects where id = object_id(N'[dbo].表名') and OBJECTPROPERTY(id, N'IsUserTable') = 1))
)
字段的具体值:
一楼的就可以,但是你必须知道要比较的两列的列名才可以本回答被提问者采纳
第3个回答  2010-05-19
if exists (select *from A where 字段 in(select 字段 from B))
print("存在")
第4个回答  2010-05-19
SELECT * FROM 表A WHERE 字段A IN (SELECT 字段B FROM 表B)
相似回答