数据库中的集合操作可以把多个查询的结果组合成一个结果集。集合操作主要包含:
这里需要注意的是参加集合操作的各查询结果的列数必须相同,对应的数据类型也必须兼容。对于 UNION 来说用户可以指定 UNION的属性为 ALL 和 DISTINCT/UNIQUE。分别代表集合可重复,和集合不可重复。而其它几种集合操作是不能指定 ALL 属性的(它们只有 DISTINCT 属性)。所有的集合操作默认的属性是 DISTINCT。在 Oceanbase 数据库中,集合操作中可以指定 ORDER BY 和 LIMIT 子句,但是不允许其他子句的出现,如下所示:
obclient> create table t1(a int primary key, b int, c int);
Query OK, 0 rows affected (0.16 sec)
obclient> create table t2(a int primary key, b int, c int);
Query OK, 0 rows affected (0.10 sec)
--支持 union 语句中出现 order by 和 limit 子句
obclient> (select * from t1 union all select * from t2) order by a limit 10;
Empty set (0.02 sec)
--不支持 union 语句中出现除 order by 和 limit 子句的其他子句,比如 group by
obclient> (select * from t1 union all select * from t2) group by a limit 10;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your OceanBase version for the right syntax to use near 'group by a limit 10' at line 1
该例子获取 t1 和 t2 中所有不重复的行。
obclient> create table t1(a int, b int, c int);
Query OK, 0 rows affected (0.12 sec)
obclient> create table t2(a int, b int, c int);
Query OK, 0 rows affected (0.11 sec)
obclient> insert into t1 values (1,1,1),(2,2,2),(3,3,3);
Query OK, 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0
obclient> insert into t2 values (2,2,2),(3,3,3),(4,4,4);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
obclient> select * from t1 union select * from t2;
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 4 |
+------+------+------+
4 rows in set (0.01 sec)
该例子获取 t1 和 t2 中的所有行,不进行去重。
obclient> create table t1(a int, b int, c int);
Query OK, 0 rows affected (0.12 sec)
obclient> create table t2(a int, b int, c int);
Query OK, 0 rows affected (0.11 sec)
obclient> insert into t1 values (1,1,1),(2,2,2),(3,3,3);
Query OK, 3 rows affected (0.07 sec)
Records: 3 Duplicates: 0 Warnings: 0
obclient> insert into t1 values (2,2,2),(3,3,3),(4,4,4);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
obclient> select * from t1 union all select * from t2;
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 2 | 2 | 2 |
| 3 | 3 | 3 |
| 4 | 4 | 4 |
+------+------+------+
6 rows in set (0.02 sec)
该例子获取同时出现在 t1 和 t2 中的行,并且去重。
obclient> create table t1(a int, b int, c int);
Query OK, 0 rows affected (0.12 sec)
obclient> create table t2(a int, b int, c int);
Query OK, 0 rows affected (0.12 sec)
obclient> insert into t1 values (1,1,1),(2,2,2),(3,3,3);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
obclient> insert into t2 values (2,2,2),(3,3,3),(3,3,3),(4,4,4);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
obclient> select * from t1 intersect select * from t2;
+------+------+------+
| a | b | c |
+------+------+------+
| 2 | 2 | 2 |
| 3 | 3 | 3 |
+------+------+------+
2 rows in set (0.01 sec)
该例子获取出现在 t1 中,但是不出现在 t2 中的行,并且去重。
obclient> create table t1(a int, b int, c int);
Query OK, 0 rows affected (0.12 sec)
obclient> create table t2(a int, b int, c int);
Query OK, 0 rows affected (0.12 sec)
obclient> insert into t1 values (1,1,1),(2,2,2),(3,3,3);
Query OK, 3 rows affected (0.02 sec)
Records: 3 Duplicates: 0 Warnings: 0
obclient> insert into t2 values (2,2,2),(3,3,3),(3,3,3),(4,4,4);
Query OK, 4 rows affected (0.01 sec)
Records: 4 Duplicates: 0 Warnings: 0
obclient> select * from t1 except select * from t2;
+------+------+------+
| a | b | c |
+------+------+------+
| 1 | 1 | 1 |
+------+------+------+
1 row in set (0.02 sec)
描述该语句用于创建数据库,并可以指定数据库的默认属性(如数据库默认字符集,校验规则等)。格式create_database_stmt:CREATE ...
ADD_MONTHS函数返回在日期date基础上n个月后的日期值,如果n的值为负数则返回日期date基础上n个月前的...
NEXT_DAY函数是返回日期d1的下一周中c1(星期值)所在的日期值。语法NEXT_DAY (d1[,c1])参数参数说明d1DATE数据类型的值。c1星期...
TO_YMINTERVAL函数将一个CHAR、VARCHAR2、NCHAR或NVARCHAR2数据类型的字符串转换为一个INTERVAL YEAR TO MONTH数据类型的值,该...
MIN函数返回参数中指定列的最小值。语法MIN([ DISTINCT | UNIQUE | ALL ] expr) [ OVER (analytic_clause) ]作为分析函数使...