表如下(name:relation)
======================
uid pid
1 test1
1 test2
1 test3
2 test1
...现在想查询出 pid有test1和test2的所有的uid
大家有什么好的解决方案么?
======================
uid pid
1 test1
1 test2
1 test3
2 test1
...现在想查询出 pid有test1和test2的所有的uid
大家有什么好的解决方案么?
调试欢乐多
test1或test2
SELECT DISTINCT "UID" FROM relation WHERE REGEXP_LIKE(pid, 'test[12]');test1和test2
SELECT a.aid
FROM (SELECT DISTINCT "UID" aid FROM relation WHERE TRIM(pid) = 'test1') a,
(SELECT DISTINCT "UID" bid FROM relation WHERE TRIM(pid) = 'test2') b
WHERE a.aid = b.bid;
如果条件过多,比如不只是test1,test2这么两箱,二十十几甚至二十几个,test1,test2,test3..... 这样的话数据库的语句会特别的长。
select count(uid) as c, uid from relation where pid in ('test1', 'test2')
group by uid
having count(uid)=2