簡體   English   中英

內部聯接在同一張桌子上

[英]Inner Join on the same table

我有2個查詢,第一個:

Select Distinct App_Form_Name,Control_Name
From Application_Form_Controls_Management
where Control_Type = 'ASPxGridView'
Order By App_Form_Name, Control_Name

它將返回(例如):

App_Form_Name | Control_Name
Form1         | ControlName1
Form1         | ControlName2 

第二:

Select Distinct App_Form_Name, Right_ID, App_Form_Function_ID
From Application_Form_Controls_Management
where Control_Type = 'ASPxGridView'
Order By App_Form_Name,Right_ID,App_Form_Function_ID

它將返回(例如):

App_Form_Name | Right_ID | App_Form_Function_ID
Form1         | 1        | 1
Form1         | 1        | 2
Form1         | 1        | 3
Form1         | 2        | 1
Form1         | 2        | 1
Form1         | 2        | 1

我想要的是Inner Join查詢,它將返回以下結果:

App_Form_Name | Control_Name | Right_ID | App_Form_Function_ID
Form1         | ControlName1 | 1        | 1
Form1         | ControlName1 | 1        | 2
Form1         | ControlName1 | 1        | 3
Form1         | ControlName1 | 2        | 1
Form1         | ControlName1 | 2        | 2
Form1         | ControlName1 | 2        | 3
Form1         | ControlName2 | 1        | 1
Form1         | ControlName2 | 1        | 2
Form1         | ControlName2 | 1        | 3
Form1         | ControlName2 | 1        | 1
Form1         | ControlName2 | 1        | 2
Form1         | ControlName2 | 1        | 3

謝謝。

您需要將查詢用作子查詢,如下所示:

Select a.App_Form_Name,a.Control_Name, b.Right_ID, b.App_Form_Function_ID
From
   (
    Select Distinct App_Form_Name,Control_Name From Application_Form_Controls_Management where Control_Type='ASPxGridView' 
   ) a
Inner join (
              Select Distinct App_Form_Name,Right_ID,App_Form_Function_ID From 
              Application_Form_Controls_Management where Control_Type='ASPxGridView'
           ) b
on a.App_Form_Name = b.App_Form_Name

如果您使用的是SQL Server,您還可以從cte結構中受益,例如:

;with cte as (
   Select Distinct App_Form_Name,Control_Name From 
   Application_Form_Controls_Management where Control_Type='ASPxGridView' 
),
cte2 as (
   Select Distinct App_Form_Name,Right_ID,App_Form_Function_ID From 
                   Application_Form_Controls_Management where Control_Type='ASPxGridView'
)
select cte.App_Form_Name,cte.Control_Name, cte2.Right_ID, cte2.App_Form_Function_ID
from cte
inner join cte2 on cte.App_Form_Name = cte2.App_Form_Name

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM