sql中,查找A表中某字段在B表中某字段中包含的记录,该怎么写

来源:百度知道 编辑:UC知道 时间:2024/06/14 11:37:08
A表,有个字段it,里面是比如YOEA,YOPI,YOMA等等
id name it
1 aa YOEA
2 BB YOPI
3 CC YOMA

B表中有两个字段,一个是code,一个是value
code value
S YOEA,YOPI
M YOMA,YOMB
现在我要查找A表中it字段的内容为B表中code为S的所有记录,该怎么查?in 貌似不可以
B表中的VALUE是'YOEA,YOPI'这样的形式,用=更不可能了

Sql Server 如下:
一:
select * from a where exists
(select 1 from b where code='S' and b.value+',' like '%'+a.it+',%' )

二:
select * from a where
(select count(1) from b where code='S' and b.value+',' like '%'+a.it+',%' ) >0

要么你数据库设计有问题。

这没法一起查,查出来的都是2表堆在一起的,实在要查的话这样
select a.*,b.* from a,b where a.it=b.code and b.code='S'

用like进行模糊查询
select * from A where it like (select code from B where code like 'S%')

select * from a,b where a.it=b.code and b.code='S'