+ -
当前位置:首页 → 问答吧 → sql向表1中追加字段信息!

sql向表1中追加字段信息!

时间:2011-12-05

来源:互联网

表1:
标题 内容 属性
1 黑色颗粒  
表2:
name word status
变色 黑 1
判断 表1.内容 是否含有 表2.word 中的关键字 然后像 表1.属性 中插入 表2.name  
哪个大神帮着看看呗 。。。 SQL语句!!!
这个肿么写 。。。

作者: gaoweiyu   发布时间: 2011-12-05

SQL code
insert into 表1(属性) select name from 表2 t where exists(select 1 from 表1 where 内容=t.word)

作者: fredrickhu   发布时间: 2011-12-05

SQL code

if object_id('a') is not null
   drop table a
go
create table a
(
 标题 int,
 内容 varchar(10),
 属性 varchar(10)
)
go
insert into a select 1,'黑色颗粒',null
go
if object_id('b') is not null
   drop table b
go
create table b
(
 name varchar(10),
 word varchar(10),
 status int
)
go
insert into b select '变色','',1
go
update a set 属性=b.name from a inner join b on b.status=a.标题 and charindex(word,内容)>0
go
select * from a
go
/*
标题          内容         属性
----------- ---------- ----------
1           黑色颗粒       变色

(1 行受影响)

*/

作者: pengxuan   发布时间: 2011-12-05

SQL code
insert into 表1(属性) select name from 表1 a ,表2 b where charindex(b.word,a.内容)>0

作者: fredrickhu   发布时间: 2011-12-05