hive (with as)

Background:
When we write some of the structure is relatively complex SQL statements, query the presence of a child may be reused in multiple places multiple levels, this time we can use it with as independent statement, which greatly improve SQL readable , simplify SQL ~
Note: currently oracle, sql server, hive etc. support with as usage, but does not support mysql!

2019-05-31 Update: MySQL8.0 large number of updates optimized to support Common table expressions, which is to support with grammar!

I. INTRODUCTION

with as also called sub-query part, first define a sql fragment, the sql fragment will be used throughout the sql statement sql statement in order to allow readability higher, as part of providing the data, and are often used in the collection of union, etc. operation.

with as it is similar to a temporary table or view can be used to store part of the sql statement as an alias, except that with as part of a one-off, and must be used in conjunction with other sql can!

Its greatest advantage is appropriate to improve the readability of the code, and if you want to repeatedly use with clause to the back, which can greatly simplify SQL; more important is: an analysis of multiple use, which is why the offer performance place, to "read less" objective.

Second, the use

The WITH T1 the AS (
the SELECT *
the FROM carinfo
),
T2 the AS (
the SELECT *
the FROM car_blacklist
)
the SELECT *
the FROM T1, T2
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
Note: this must be a whole as a sql query, i.e. with as statement after You can not add a semicolon, or will be error.

Third, pay attention

1. with clause must be defined before the select statement referenced at the same level with keywords can only be used once, more than only separated by commas; the last one with between clauses and the following query can not have a comma, only by the right parenthesis split, with clauses of the query must be enclosed in parentheses.

The following wording will complain:

AS T1 with (SELECT * from carinfo)
with T2 AS (SELECT * from car_blacklist)
SELECT * from T1, T2
. 1
2
. 3
with T1 AS (SELECT * from carinfo);
SELECT * from T1
. 1
2
2. If the sub-defined with sentence, but then there is no query with select, you will be given!

The following wording will complain:

AS t1 with (select * from carinfo)
1
correct wording (t1 is not used does not matter, there followed select the line):

AS T1 with (SELECT * from carinfo)
SELECT * from carinfo
. 1
2
3. clause defines the query with the foregoing may be used in the back with clause. But with a clause with clause can not be nested inside!

Correct wording:

AS T1 with (the SELECT * from carinfo),
T2 AS (t1.id from the SELECT T1)
the SELECT * from T2
----------------
Disclaimer: This article is CSDN bloggers' deep Han Dian "in the original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Original link: https: //blog.csdn.net/Abysscarry/article/details/81322669

Guess you like

Origin www.cnblogs.com/jeasonit/p/11600083.html