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

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'这样的形式,用=更不可能了

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

这没法一起查,查出来的都是2表堆在一起的,实在要查的话这样
select a.*,b.* from a,b where a.it=b.code and b.code='S'
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-04-15
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个回答  2009-01-15
用like进行模糊查询
select * from A where it like (select code from B where code like 'S%')
第3个回答  2009-01-15
select * from a,b where a.it=b.code and b.code='S'
相似回答