SQL-- WITH AS (filtering must be done after querying complex data)

[Use and definition]

Use when the query structure is complex and reuse a subquery sql multiple times to cause repeated redundancy. Separating the subquery statement can be regarded as an assignment usage

ORACLE, SQL SERVER, HIVE, etc. can all be used, and after MYSQL8.0 has been optimized, it can also be used

【Features】

Stored in the user's temporary table space, analyzed once and used many times

One of the ways to enhance readability, decoupling, and optimize query efficiency

【achieve】

WITH AAA AS
(
    SELECT
        COALESCE(A.student_name,'空') sname,
        COALESCE(B.teacher_name,'空') tname,
    FROM
        Student A 
    JOIN
        Teacher B
    ON
        A.CLASS_ID=B.CLASS_ID
),
CCC AS
(
    SELECT
        count(*)
    FROM
        Student A
    WHERE
        A.sex = '男'
        AND
        A.class_name = '一年级一班'
)
SELECT
    AAA.sname,
    AAA.tname,
    CCC.cname
FROM
    AAA,CCC
    

【Note】

  1. with as and subsequent select are used as a sql
  2. A SQL can only have one with, and multiple ass are separated by ",", and the end of in is ")"
  3. with cannot be used alone or nested

The article reference great god link is as follows

https://blog.csdn.net/Abysscarry/article/details/81322669?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase

 

Guess you like

Origin blog.csdn.net/qq_36766417/article/details/106684462