如何编写自联接查询? [英] How to write selfjoin query?
问题描述
MID-名称-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
上面是表结构.
我想编写一个查询(条件将为PID = 2),将从中获取以下值
11
12
14
31
32
33
35
有人可以帮我写这个查询吗?
在此先感谢.
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.
推荐答案
从表名中选择t1.MID作为t1
在t1.PID = t2.PID上将表名加入为t2
其中t1.PID = 2
select t1.MID from tablename as t1
join tablename as t2 on t1.PID = t2.PID
where t1.PID = 2
select Mid from urtable where pid=2
查询什么问题.它会给你同样的结果.如果要编写自连接查询,请使用解决方案1.
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.
这篇关于如何编写自联接查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!