简体   繁体   中英

How to write SQL query for the scenario?

I have a table named demo1 with primary_id is ID and the values are as below

ID    Name 
1       A
2       B
3       C
4       D
5       E 

Another table is demo2 which has following data with the primary key ID1

ID1   I1 P1 P2
10     1  2  3
20     2  1  5

Values of I1, P1, and P2 is from field ID of table demo1 Now i have the value of ID1 of demo2 table and I have to get value of Name field of demo1 table with respect to ID : I1, P1, and P2 for example if i know 10(value of ID1) then I should get the output as follows

ID1    I1_NAME I2_NAME  I3_NAME
10        A        B      C

What could be the sql query for this?

Thanks

select id1, d1.name as i1_name, d2.name as i2_name, d3.name as i3_name
from demo2, demo1 as d1, demo1 as d2, demo1 as d3
where demo2.i1 = d1.id, demo2.p1 = d2.id, demo2.p2 = d3.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM