SELECT clause with self joining table

前端 未结 3 541
盖世英雄少女心
盖世英雄少女心 2021-01-15 18:12

Two tables:

//SKILL
SNAME
---------------------
C++
C#
C
JAVA

//REQUIRED
SNAME          REQUIRED
------------------------
C++               C
C#                     


        
3条回答
  •  既然无缘
    2021-01-15 19:01

    The join criteria

    ON sq.SNAME='C++'; 
    

    Does not relate the two tables (sq and sq1) , and limits the result set to sq.SNAME = 'C++', which is why you are gettig only SNAME = 'C++' in the output.

    The join would need to be as follows.

    ON sq.Requires = sq1.sName 
    

    Then in your Where clause you need to specify:

    WHERE sq.sNAME = 'C++'
    

    You will then have sq.sName with 'C++' in 2 lines. sq1.sName with 'c' in all columns. and sq1.Requires with 'Reading' and 'Writing'

    To get the result set in a single output you will have to use a UNION or a CTE. a UNION will give you the 2 levels you need. a CTE can give you n-levels. i.e if reading had another per-requisite.

提交回复
热议问题