所以我有这个问题,我需要找到coloumn具有specfic值的行。我使用的表是这样创建的:
select table.CID, table2.PID
from table
inner join table2 on table2.OID = table.OID
表格如下:
+------------ table --------------+
|____table.CID___|___table2.PID___|
| AA | 47 |
| AA | 25 |
| AA | 13 |
| AA | 18 |
| AB | 22 |
| AB | 89 |
| AC | 47 |
| AC | 15 |
| AC | 8 |
+---------------------------------+
现在我想得到table.CID相等的所有行,它至少有一个table.PID为47.结果:
+------------ table --------------+
|____table.CID___|___table2.PID___|
| AA | 47 |
| AA | 25 |
| AA | 13 |
| AA | 18 |
| AC | 47 |
| AC | 15 |
| AC | 8 |
+---------------------------------+
怎么会这样做?我试过where exists
,但没有运气。
答案 0 :(得分:1)
此查询查找TreatTinyAsBoolean=false
的行:
47
然后可以使用这些行的SELECT ...
FROM MyTable JOIN Table2 USING (OID)
WHERE Table2.PID = 47;
值来过滤原始查询:
CID
答案 1 :(得分:0)
使用exists
:
select t.*
from table t
where exists (select 1 from table t2 where t2.cid = t.cid and t2.pid = 47);
答案 2 :(得分:0)
假设您只想要来自table.cid的多行匹配的行:
SELECT table.CID, table2.PID
FROM table INNER JOIN table2 on table2.OID = table.OID
WHERE ( SELECT count(*)
FROM table tt INNER JOIN table2 tt2 ON tt2.OID = tt.OID
WHERE tt.cid = table.cid ) > 1
And Exists ( SELECT 1
FROM table2 tt2 WHERE tt2.OID = table.OID And tt2.pid = 47 )