Skip to content
Snippets Groups Projects
Unverified Commit 0ed88cc0 authored by prinz's avatar prinz Committed by GitHub
Browse files

Add constraint for CTE, update sql faq and edit subquries docs (#4171)

* Add constraint for CTE, update sql faq and edit subquries docs

* update subquery overview

* rename subquery with all
parent 66b37458
No related branches found
No related tags found
No related merge requests found
Showing
with 438 additions and 4 deletions
......@@ -4,6 +4,8 @@
不区分大小写。
在 MatrixOne 中,只有一种情况需要区分大小写:如果你创建的表和属性带有 \`\`\`\` 中的名称需要注意大小写。查询这个表名或属性名,那么表名和属性名也需要被包含在\`\`里。
* **如何将数据从 MatrixOne 导出到文件?**
你可以使用 `SELECT INTO OUTFILE` 命令来将数据导出为 **csv** 文件(只能导出到服务器主机,无法到远程客户端)。
......
# **子查询与比较操作符的使用**
## **语法描述**
子查询与比较操作符最常见的用法如下:
```
non_subquery_operand comparison_operator (subquery)
```
其中,`comparison_operator` 指以下操作符:
```
= > < >= <= <> != <=>
```
## **语法结构**
```
> SELECT column_name(s) FROM table_name WHERE 'a' = (SELECT column1 FROM t1)
```
## **示例**
```sql
> create table t1 (a int);
> create table t2 (a int, b int);
> create table t3 (a int);
> create table t4 (a int not null, b int not null);
> insert into t1 values (2);
> insert into t2 values (1,7),(2,7);
> insert into t4 values (4,8),(3,8),(5,9);
> insert into t3 values (6),(7),(3);
> select * from t3 where a = (select b from t2);
+------+
| a |
+------+
| 7 |
| 7 |
+------+
2 rows in set (0.01 sec)
```
# FROM子查询 (beta)
# **Derived Tables**
## **语法描述**
......@@ -38,3 +38,44 @@
+------+------+--------+
2 rows in set (0.02 sec)
```
- **Subquery with Join**:
```sql
> create table t1 (libname1 varchar(21) not null primary key, city varchar(20));
> create table t2 (isbn2 varchar(21) not null primary key, author varchar(20), title varchar(60));
> create table t3 (isbn3 varchar(21) not null, libname3 varchar(21) not null, quantity int);
> insert into t2 values ('001','Daffy','Aducklife');
> insert into t2 values ('002','Bugs','Arabbitlife');
> insert into t2 values ('003','Cowboy','Lifeontherange');
> insert into t2 values ('000','Anonymous','Wannabuythisbook?');
> insert into t2 values ('004','BestSeller','OneHeckuvabook');
> insert into t2 values ('005','EveryoneBuys','Thisverybook');
> insert into t2 values ('006','SanFran','Itisasanfranlifestyle');
> insert into t2 values ('007','BerkAuthor','Cool.Berkley.the.book');
> insert into t3 values('000','NewYorkPublicLibra',1);
> insert into t3 values('001','NewYorkPublicLibra',2);
> insert into t3 values('002','NewYorkPublicLibra',3);
> insert into t3 values('003','NewYorkPublicLibra',4);
> insert into t3 values('004','NewYorkPublicLibra',5);
> insert into t3 values('005','NewYorkPublicLibra',6);
> insert into t3 values('006','SanFransiscoPublic',5);
> insert into t3 values('007','BerkeleyPublic1',3);
> insert into t3 values('007','BerkeleyPublic2',3);
> insert into t3 values('001','NYC Lib',8);
> insert into t1 values ('NewYorkPublicLibra','NewYork');
> insert into t1 values ('SanFransiscoPublic','SanFran');
> insert into t1 values ('BerkeleyPublic1','Berkeley');
> insert into t1 values ('BerkeleyPublic2','Berkeley');
> insert into t1 values ('NYCLib','NewYork');
> select * from (select city,libname1,count(libname1) as a from t3 join t1 on libname1=libname3 join t2 on isbn3=isbn2 group by city,libname1) sub ;
+----------+--------------------+------+
| city | libname1 | a |
+----------+--------------------+------+
| NewYork | NewYorkPublicLibra | 6 |
| SanFran | SanFransiscoPublic | 1 |
| Berkeley | BerkeleyPublic1 | 1 |
| Berkeley | BerkeleyPublic2 | 1 |
+----------+--------------------+------+
4 rows in set (0.00 sec)
```
# **Subqueries with ALL**
## **语法描述**
The word `ALL`, which must follow a comparison operator, means “return `TRUE` if the comparison is `TRUE` for `ALL` of the values in the column that the subquery returns.”:
关键词 `ALL` 必须跟在比较操作符后面,指如果子查询返回的列中值的 `ALL` 的比较是 `TRUE`,则返回 `TRUE`
```
operand comparison_operator ALL (subquery)
```
示例如下:
```
SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);
```
如上述示例中,假设表 t1 中有一行包含(10),表 t2 包含(-5,0,+5),则表达式为 `TRUE`,因为10大于 t2 中的所有三个值。如果表 t2 包含(12,6,NULL,-100),则表达式为 `FALSE`,因为在表 t2 中有一个大于10的值12。如果表 t2 包含(0,NULL,1),则表达式为 `NULL`
- 如果表 t2 为空,则表达式为 `TRUE`。例如,当下表 t2 为空时,表达式是 `TRUE`
```
SELECT * FROM t1 WHERE 1 > ALL (SELECT s1 FROM t2);
```
- 下面示例中,当表 t2 为空时,这个表达式是 `NULL`
```
SELECT * FROM t1 WHERE 1 > (SELECT s1 FROM t2);
```
或:
```
SELECT * FROM t1 WHERE 1 > ALL (SELECT MAX(s1) FROM t2);
```
**说明**:在书写子查询语法时,要注意考虑到含有 `NULL` 值的表和空表的情况。
## **语法结构**
```
> SELECT column_name(s) FROM table_name {WHERE | HAVING} [not] expression comparison_operator ALL (subquery)
```
## **示例**
```sql
> create table t1 (a int);
> create table t2 (a int, b int);
> create table t3 (a int);
> create table t4 (a int not null, b int not null);
> create table t5 (a int);
> create table t6 (a int, b int);
> insert into t1 values (2);
> insert into t2 values (1,7),(2,7);
> insert into t4 values (4,8),(3,8),(5,9);
> insert into t5 values (null);
> insert into t3 values (6),(7),(3);
> insert into t6 values (10,7),(null,7);
> select * from t3 where a <> all (select b from t2);
+------+
| a |
+------+
| 6 |
| 3 |
+------+
2 rows in set (0.00 sec)
> select * from t4 where 5 > all (select a from t5);
+------+------+
| a | b |
+------+------+
| 4 | 8 |
| 3 | 8 |
| 5 | 9 |
+------+------+
3 rows in set (0.01 sec)
> select * from t3 where 10 > all (select b from t2);
+------+
| a |
+------+
| 6 |
| 7 |
| 3 |
+------+
3 rows in set (0.00 sec)
```
......@@ -26,7 +26,9 @@ SELECT * FROM t1 WHERE column1 = (SELECT column1 FROM t2);
更多信息,参见:
- [FROM 子查询](subqueries/from-subquery.md)
- [子查询与 ANY 操作符的使用](subqueries/subquery-with-any.md)
- [派生表](subqueries/derived-tables.md)
- [子查询与比较操作符的使用](subqueries/comparisons-using-subqueries.md)
- [子查询与 ANY 或 SOME 操作符的使用](subqueries/subquery-with-any-some.md)
- [子查询与 ALL 操作符的使用](subqueries/subquery-with-all.md)
- [子查询与 EXISTS 操作符的使用](subqueries/subquery-with-exists.md)
- [子查询与 IN 操作符的使用](subqueries/subquery-with-in.md)
......@@ -58,3 +58,7 @@ AS
+------+------------------------------------------------------+
5 rows in set (0.01 sec)
```
## **限制**
MatrixOne 暂不支持递归 CTE。
# **Comparisons Using Subqueries**
## **Description**
The most common use of a subquery is in the form:
```
non_subquery_operand comparison_operator (subquery)
```
Where comparison_operator is one of these operators:
```
= > < >= <= <> != <=>
```
## **Syntax**
```
> SELECT column_name(s) FROM table_name WHERE 'a' = (SELECT column1 FROM t1)
```
## **Examples**
```sql
> create table t1 (a int);
> create table t2 (a int, b int);
> create table t3 (a int);
> create table t4 (a int not null, b int not null);
> insert into t1 values (2);
> insert into t2 values (1,7),(2,7);
> insert into t4 values (4,8),(3,8),(5,9);
> insert into t3 values (6),(7),(3);
> select * from t3 where a = (select b from t2);
+------+
| a |
+------+
| 7 |
| 7 |
+------+
2 rows in set (0.01 sec)
```
# **Subqueries in the FROM Clause (beta)**
# **Derived Tables**
## **Description**
......@@ -38,3 +38,44 @@ Every table in a FROM clause must have a name, therefore the [AS] name clause is
+------+------+--------+
2 rows in set (0.02 sec)
```
- **Subquery with Join**:
```sql
> create table t1 (libname1 varchar(21) not null primary key, city varchar(20));
> create table t2 (isbn2 varchar(21) not null primary key, author varchar(20), title varchar(60));
> create table t3 (isbn3 varchar(21) not null, libname3 varchar(21) not null, quantity int);
> insert into t2 values ('001','Daffy','Aducklife');
> insert into t2 values ('002','Bugs','Arabbitlife');
> insert into t2 values ('003','Cowboy','Lifeontherange');
> insert into t2 values ('000','Anonymous','Wannabuythisbook?');
> insert into t2 values ('004','BestSeller','OneHeckuvabook');
> insert into t2 values ('005','EveryoneBuys','Thisverybook');
> insert into t2 values ('006','SanFran','Itisasanfranlifestyle');
> insert into t2 values ('007','BerkAuthor','Cool.Berkley.the.book');
> insert into t3 values('000','NewYorkPublicLibra',1);
> insert into t3 values('001','NewYorkPublicLibra',2);
> insert into t3 values('002','NewYorkPublicLibra',3);
> insert into t3 values('003','NewYorkPublicLibra',4);
> insert into t3 values('004','NewYorkPublicLibra',5);
> insert into t3 values('005','NewYorkPublicLibra',6);
> insert into t3 values('006','SanFransiscoPublic',5);
> insert into t3 values('007','BerkeleyPublic1',3);
> insert into t3 values('007','BerkeleyPublic2',3);
> insert into t3 values('001','NYC Lib',8);
> insert into t1 values ('NewYorkPublicLibra','NewYork');
> insert into t1 values ('SanFransiscoPublic','SanFran');
> insert into t1 values ('BerkeleyPublic1','Berkeley');
> insert into t1 values ('BerkeleyPublic2','Berkeley');
> insert into t1 values ('NYCLib','NewYork');
> select * from (select city,libname1,count(libname1) as a from t3 join t1 on libname1=libname3 join t2 on isbn3=isbn2 group by city,libname1) sub ;
+----------+--------------------+------+
| city | libname1 | a |
+----------+--------------------+------+
| NewYork | NewYorkPublicLibra | 6 |
| SanFran | SanFransiscoPublic | 1 |
| Berkeley | BerkeleyPublic1 | 1 |
| Berkeley | BerkeleyPublic2 | 1 |
+----------+--------------------+------+
4 rows in set (0.00 sec)
```
# **Subqueries with ALL**
## **Description**
The word `ALL`, which must follow a comparison operator, means “return `TRUE` if the comparison is `TRUE` for `ALL` of the values in the column that the subquery returns.”:
```
operand comparison_operator ALL (subquery)
```
For example:
```
SELECT s1 FROM t1 WHERE s1 > ALL (SELECT s1 FROM t2);
```
Suppose that there is a row in table t1 containing (10). The expression is `TRUE` if table t2 contains (-5,0,+5) because 10 is greater than all three values in t2. The expression is `FALSE` if table t2 contains (12,6,NULL,-100) because there is a single value 12 in table t2 that is greater than 10, and the result returns `Empty set`. The expression is unknown (that is, `NULL`) if table t2 contains (0,NULL,1).
Finally, the expression is `TRUE` if table t2 is empty. So, the following expression is `TRUE` when table t2 is empty:
```
SELECT * FROM t1 WHERE 1 > ALL (SELECT s1 FROM t2);
```
But this expression is `NULL` when table t2 is empty:
```
SELECT * FROM t1 WHERE 1 > (SELECT s1 FROM t2);
```
In addition, the following expression is NULL when table t2 is empty:
```
SELECT * FROM t1 WHERE 1 > ALL (SELECT MAX(s1) FROM t2);
```
In general, tables containing `NULL` values and empty tables are “edge cases.” When writing subqueries, always consider whether you have taken those two possibilities into account.
## **Syntax**
```
> SELECT column_name(s) FROM table_name {WHERE | HAVING} [not] expression comparison_operator ALL (subquery)
```
## **Examples**
```sql
> create table t1 (a int);
> create table t2 (a int, b int);
> create table t3 (a int);
> create table t4 (a int not null, b int not null);
> create table t5 (a int);
> create table t6 (a int, b int);
> insert into t1 values (2);
> insert into t2 values (1,7),(2,7);
> insert into t4 values (4,8),(3,8),(5,9);
> insert into t5 values (null);
> insert into t3 values (6),(7),(3);
> insert into t6 values (10,7),(null,7);
> select * from t3 where a <> all (select b from t2);
+------+
| a |
+------+
| 6 |
| 3 |
+------+
2 rows in set (0.00 sec)
> select * from t4 where 5 > all (select a from t5);
+------+------+
| a | b |
+------+------+
| 4 | 8 |
| 3 | 8 |
| 5 | 9 |
+------+------+
3 rows in set (0.01 sec)
> select * from t3 where 10 > all (select b from t2);
+------+
| a |
+------+
| 6 |
| 7 |
| 3 |
+------+
3 rows in set (0.00 sec)
```
......@@ -26,7 +26,9 @@ A subquery may occur in:
For more information, see the reference below:
- [FROM SUBQUERY](subqueries/from-subquery.md)
- [SUBQUERY WITH ANY](subqueries/subquery-with-any.md)
- [Derived Tables](subqueries/derived-tables.md)
- [Comparisons Using Subqueries](subqueries/comparisons-using-subqueries.md)
- [SUBQUERY WITH ANY or SOME](subqueries/subquery-with-any-some.md)
- [SUBQUERY WITH ALL](subqueries/subquery-with-all.md)
- [SUBQUERY WITH EXISTS](subqueries/subquery-with-exists.md)
- [SUBQUERY WITH IN](subqueries/subquery-with-in.md)
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment