jeudi 8 octobre 2015

Find rows based on two coloumns in two tables

So I have this problem where I need to find the rows where a coloumn have a specfic value. The table I use is created like this:

select table.CID, table2.PID

from table

inner join table2 on table2.OID = table.OID

The table looks like this:

+------------ table --------------+
|____table.CID___|___table2.PID___|  
|       AA       |       47       |  
|       AA       |       25       |  
|       AA       |       13       |  
|       AA       |       18       |  
|       AB       |       22       |  
|       AB       |       89       |  
|       AC       |       47       |
|       AC       |       15       |
|       AC       |        8       |
+---------------------------------+

Now I want to get all rows where table.CID is equal and it has atleast one table.PID that is 47. Results in:

 +------------ table --------------+
|____table.CID___|___table2.PID___|  
|       AA       |       47       |  
|       AA       |       25       |  
|       AA       |       13       |  
|       AA       |       18       |  
|       AC       |       47       |
|       AC       |       15       |
|       AC       |        8       |
+---------------------------------+

How would one do this? I've tried with where exists but with no luck.

Aucun commentaire:

Enregistrer un commentaire