• 联级查询 id 和 parentid 左外查询


    CREATE TABLE #t1 (id VARCHAR(10),name VARCHAR(100))
    CREATE TABLE #t2 ( id VARCHAR(10), parentid VARCHAR(10), name VARCHAR(100) )
    
    INSERT INTO #t1 ( id, name ) VALUES('1','asdfad');
    INSERT INTO #t1 ( id, name ) VALUES('2','送达方');
    INSERT INTO #t1 ( id, name ) VALUES('3','阿飞');
    INSERT INTO #t1 ( id, name ) VALUES('4','可考虑');
    INSERT INTO #t1 ( id, name ) VALUES('5','发放人');
    
    INSERT INTO #t2 ( id,parentid, name ) VALUES('1','','1ff');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('2','','2上等');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('3','','3㐇');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('4','','4热');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('5','1','5发给');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('6','1','6是');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('7','1','7的');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('8','1','8飞');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('9','','9人');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('0','','10E店宝');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('11','5','11去');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('12','5','124 就');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('13','5','13 你');
    INSERT INTO #t2 ( id,parentid, name ) VALUES('14','5','14了');
    
    GO
    
    SELECT a.id AS a_id,a.name AS a_name ,
    b.id AS b_id,b.parentid AS b_parentid,b.name AS b_name,
    c.id AS b_id,c.parentid AS c_parentid,c.name AS c_name
    FROM #t1 AS a,
    #t2 AS b
    LEFT JOIN #t2 AS c ON b.parentid = c.id -- 子记录,使用父id 去关联父记录
    WHERE a.id = b.id

  • 相关阅读:
    大聊Python----SocketServer
    2、MySQL常见数据库引擎及比较?
    大聊Python----通过Socket实现简单的ssh客户端
    1、列举常见的关系型数据库和非关系型都有那些?
    uva12563 Jin Ge Jin Qu hao(01背包)
    UVA 12174 Shuffle(滑动窗口)
    C++中substr函数的用法
    uva11078
    11462 Age Sort(计数排序)
    UVA 11988 Broken Keyboard (a.k.a. Beiju Text) (链表,模拟)
  • 原文地址:https://www.cnblogs.com/lanyubaicl/p/15896586.html
Copyright © 2020-2023  润新知