Click here to Skip to main content
15,905,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
MID -- Name-- PID
2 ---- A ----- 0
3 ---- M ----- 0
4 ---- N ----- 0
11 --- B ----- 2
12 --- C ----- 2
13 --- D ----- 3
14 --- E ----- 2
31 --- Z ---- 11
32 --- Y ---- 12
33 --- X ---- 12
34 --- W ---- 13
35 --- V ---- 14

Above is the table structure.
I want to write a single query (where condition will be PID=2) from which this following value will be fetched

11
12
14
31
32
33
35

Can anyone help me to write this query?
Thanks in advance.
Posted
Updated 29-Jun-12 18:51pm
v2

select t1.MID from tablename as t1
join tablename as t2 on t1.PID = t2.PID
where t1.PID = 2
 
Share this answer
 
Comments
Prosan 30-Jun-12 1:03am    
my 5
sahabiswarup 30-Jun-12 1:47am    
you skip my table record..i want to fetch records where PID=2
and then select those MID ie 11,12,14
and then select MID where PID 11,12,14

then finally i'll get
11
12
14
31
32
33
35
SQL
select Mid from urtable where pid=2

what problem in query. it will give you same result. no need of selfjoin if you want to write self join query use solution 1.
 
Share this answer
 
Comments
sahabiswarup 30-Jun-12 1:49am    
first of all select MID where PID=2
then result will be 11,12,14
Then select MID where PID=11,12,14
then result will be 31,32,33,35
so finally the result will be
11 12 14 31 32 33 35
got it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900