[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】
- with as and subsequent select are used as a sql
- A SQL can only have one with, and multiple ass are separated by ",", and the end of in is ")"
- with cannot be used alone or nested
The article reference great god link is as follows