简体   繁体   English

如何编写该场景的SQL查询?

[英]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 我有一个名为demo1的表,其primary_id为ID,其值如下

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 另一个表是demo2,它具有以下具有主键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 I1,P1和P2的值来自表demo1的字段ID现在,我具有demo2表的ID1的值,并且我必须获取关于ID的demo1表的Name字段的值:例如I1,P1和P2如果我知道10(ID1的值),那么我应该得到如下输出

ID1    I1_NAME I2_NAME  I3_NAME
10        A        B      C

What could be the sql query for this? sql查询可能是什么?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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