怎么用sql语句,匹配多行数据的同一个字段
SQL code
create table mainTable
(
mainId int
);
create table tagTable
(
tagId int,
tagName varchar(15)
);
create table relationTable
(
mainId int,
tagId int
);
基本结构就是这样了.mainTable是主数据表,tagTable是标签信息的数据表,relationTable记录mainTable的所有标签关系,单向的一对多关系.
能否利用sql语句,匹配同时包含某几个tagName的mainId
------解决方案--------------------select mainId
from relationTable
where tagId in
(
select tagId from tagTable where tagName in ('A','B','C')
)
------解决方案--------------------如果要找mainTable中数据,那么再套一层
select * from mainTable
where mainId
in
(
select mainId
from relationTable
where tagId in
(
select tagId from tagTable where tagName in ('A','B','C')
)
)