How to get result from parent child table

前端 未结 4 1074
一个人的身影
一个人的身影 2021-01-22 14:44

Work on SQL-Server. My table structure is below

CREATE TABLE [dbo].[AgentInfo](
    [AgentID] [int] NOT NULL,
    [ParentID] [int] NULL,
 CONST         


        
相关标签:
4条回答
  • 2021-01-22 14:48

    Try something like this:

    WITH Merged (AgentId, ParentId) AS (
         SELECT AgentId, ParentId FROM AgentInfo WHERE ParentId = -1
         UNION ALL
         SELECT AgentInfo.AgentId, AgentInfo.ParentId FROM AgentInfo INNER JOIN Merged ON AgentInfo.AgentId = Merged.ParentId
    )
    SELECT * FROM Merged
    
    0 讨论(0)
  • 2021-01-22 15:03

    You could use a recursive SELECT, see the examples in the documentation for WITH, starting with example D.

    The general idea within the recursive WITH is: You have a first select that is the starting point, and then a UNION ALL and a second SELECT which describes the step from on level to the next, where the previous level can either be the result of the first select or the result of the previous run of the second SELECT.

    0 讨论(0)
  • 2021-01-22 15:12

    You can try this, to get a tree of the elements:

    WITH CTE_AgentInfo(AgentID, ParentID, BaseAgent)
    AS(
      SELECT 
        AgentID,
        ParentID,
        AgentID AS BaseAgent
       FROM AgentInfo
       WHERE ParentID = -1
      UNION ALL
         SELECT 
        a.AgentID,
        a.ParentID,
        a.AgentID AS BaseAgent
       FROM AgentInfo a
       INNER JOIN CTE_AgentInfo c ON
        c.AgentID = a.ParentID
      )
    SELECT * FROM CTE_AgentInfo
    

    And here is an SQLFiddle demo to see it.

    0 讨论(0)
  • 2021-01-22 15:12

    You can use a Common Table Expression to do this.

    The sql statement will then look like this:

    WITH [Parents]([AgentID], [ParentID], [BaseAgent])
    AS
    (
        SELECT 
          [AgentID],
          [ParentID],
          [AgentID] AS [BaseAgent]
        FROM [AgentInfo]
        WHERE [ParentID] = -1
        UNION ALL
        SELECT 
          [ai].[AgentID],
          [ai].[ParentID],
          [p].[BaseAgent]
        FROM [AgentInfo] [ai]
        INNER JOIN [Parents] [p] 
          ON [ai].[ParentID] = [p].[AgentID]
    )
    
    SELECT *
    FROM [Parents]
    ORDER BY 
      [BaseAgent] ASC,
      [AgentID] ASC
    

    But, the results are different from your desired output, since every Agent is only listed once. The output is:

    AGENTID     PARENTID    BASEAGENT
    1           -1          1
    3            1          1
    2           -1          2
    4            2          2
    

    The Fiddle is over here.

    And here is a nice post on working with hierarchies: What are the options for storing hierarchical data in a relational database?

    0 讨论(0)
提交回复
热议问题