Mysql查询 麻烦看清楚题目再答

来源:百度知道 编辑:UC知道 时间:2024/05/18 07:34:54
有一张表table含有两个字段caseid、tagid,没有主键。
tagid可以对应多个caseid,caseid也可以对应多个tagid;

现在需要查询所有对应tagid=1 而且同时对应 tagid=2 的 caseid;
一定要对应了两个tagid的caseid才选取出来!!!!!

mysql 查询语句怎么写?
比如:

caseid tagid
12 1
16 1
34 1
12 2
54 2
16 3

那么同时对应tagid 1、2 的caseid就是12了。

select caseid from table1 where tagid=1 or tagid=2 group by caseid having count(caseid)>1

我冤呀!!!!!

select caseid from 表名 where tagid = 1 and caseid in ( SELECT caseid FROM 表名 WHERE tagid=2 )

to : badkano 我来了呵呵!

select a.caseid from (select caseid from table1 where tagid=1) a full outer join (select caseid from table1 where tagid=2) b on a.caseid=b.caseid

to:mxm123

对不对啊!

SELECT * FROM table WHERE tagid=1 AND caseid=(SELECT tagid FROM TABLE WHERE tagid=2)

select a.caseid from (select distcint caseid from table where tagid=1) a,(select distcint caseid from table where tagid=2) b where a.caseid=b.caseid