SELECT 的语法相对比较复杂。本节首先会介绍普通的 SELECT 语法结构,然后介绍集合类 SELECT 的语法结构。
该语句用于查询表中的内容。
simple_select:
SELECT [/*+ hint statement */] [ALL | DISTINCT | UNIQUE | SQL_CALC_FOUND_ROWS]
select_expr_list FROM from_list [WHERE condition]
[GROUP BY group_expression_list [WITH ROLLUP] [HAVING condition]]
[ORDER BY order_expression_list]
[limit_clause]
[FOR UPDATE]
select_expr:
table_name.*
| table_alias_name.*
| expr [[AS] column_alias_name]
from_list:
table_reference [, table_reference ...]
table_reference:
simple_table
| joined_table
simple_table:
table_factor [partition_option] [[AS] table_alias_name]
| (select_stmt) [AS] table_alias_name
| (table_reference_list)
joined_table:
table_reference [INNER] JOIN simple_table [join_condition]
| table_reference outer_join_type JOIN simple_table join_condition
partition_option:
PARTITION (partition_name_list)
partition_name_list:
partition_name [, partition_name ...]
outer_join_type:
{LEFT | RIGHT | FULL} [OUTER]
join_condition:
ON expression
condition:
expression
group_expression_list:
group_expression [, group_expression ...]
group_expression:
expression [ASC | DESC]
order_expression_list:
order_expression [, order_expression ...]
order_expression:
expression [ASC | DESC]
limit_clause:
LIMIT {[offset,] row_count |row_count OFFSET offset}
参数 | 描述 |
---|---|
ALL | DISTINCT | UNIQUE | SQL_CALC_FOUND_ROWS | 在数据库表中,可能会包含重复值。
|
select_expr | 列出要查询的表达式或列名,用“,”隔开。也可以用“*”表示所有列。 |
AS othername | 为输出字段重新命名。 |
FROM table_references | 指名了从哪个表或哪些表中读取数据(支持多表查询)。 |
WHERE where_conditions | 可选项,WHERE 字句用来设置一个筛选条件,查询结果中仅包含满足条件的数据。where_conditions 为表达式。 |
GROUP BY group_by_list | 用于进行分类汇总。 |
HAVING search_confitions | HAVING 字句与 WHERE 字句类似,但是 HAVING 字句可以使用累计函数(如 SUM,AVG 等)。 |
ORDER BY order_list order_list : colname [ASC | DESC] [,colname [ASC | DESC]…] | 用来按升序(ASC)或者降序(DESC)显示查询结果。不指定 ASC 或者 DESC 时,默认为 ASC。 |
[LIMIT {[offset,] row_count |row_count OFFSET offset}] | 强制 SELECT 语句返回指定的记录数。 LIMIT 接受一个或两个数字参数。参数必须是一个整数常量。
|
FOR UPDATE | 对查询结果所有行上排他锁,以阻止其他事务的并发修改,或阻止在某些事务隔离级别时的并发读取。 |
PARTITION(partition_list) | 指定查询表的分区信息。例如:partition(p0,p1…) |
以如下表 a 为例。
obclient> SELECT name FROM a;
+------+
| name |
+------+
| a |
| b |
| a |
+------+
3 rows in set (0.01 sec)
obclient> SELECT DISTINCT name FROM a;
+------+
| name |
+------+
| a |
| b |
+------+
2 rows in set (0.01 sec)
obclient> SELECT id, name, num/2 AS avg FROM a;
+------+------+----------+
| id | name | avg |
+------+------+----------+
| 1 | a | 50.0000 |
| 2 | b | 100.0000 |
| 3 | a | 25.0000 |
+------+------+----------+
3 rows in set (0.01 sec)
obclient> SELECT id, name, num FROM a WHERE name = 'a';
+------+------+------+
| id | name | num |
+------+------+------+
| 1 | a | 100 |
| 3 | a | 50 |
+------+------+------+
2 rows in set (0.01 sec)
obclient> SELECT id, name, SUM(num) FROM a GROUP BY name;
+------+------+----------+
| id | name | SUM(num) |
+------+------+----------+
| 1 | a | 150 |
| 2 | b | 200 |
+------+------+----------+
2 rows in set (0.00 sec)
obclient> SELECT id, name, SUM(num) as sum FROM a GROUP BY name HAVING SUM(num) < 160;
+------+------+------+
| id | name | sum |
+------+------+------+
| 1 | a | 150 |
+------+------+------+
1 row in set (0.01 sec)
obclient> SELECT * FROM a ORDER BY num ASC;
+------+------+------+
| id | name | num |
+------+------+------+
| 3 | a | 50 |
| 1 | a | 100 |
| 2 | b | 200 |
+------+------+------+
3 rows in set (0.00 sec)
obclient> SELECT * FROM a ORDER BY num DESC;
+------+------+------+
| id | name | num |
+------+------+------+
| 2 | b | 200 |
| 1 | a | 100 |
| 3 | a | 50 |
+------+------+------+
3 rows in set (0.00 sec)
obclient> SELECT * FROM a LIMIT 1,2;
+------+------+------+
| id | name | num |
+------+------+------+
| 2 | b | 200 |
| 3 | a | 50 |
+------+------+------+
2 rows in set (0.00 sec)
该语句用于对多个 SELECT 查询的结果进行 UNION,MINUS,INTERSECT。
select_clause_set:
simple_select [ UNION | UNION ALL | EXCEPT | INTERSECT] select_clause_set_left
[ORDER BY sort_list_columns] [limit_clause]
select_clause_set_right:
simple_select |
select_caluse_set
参数 | 描述 |
---|---|
UNION ALL | 合并两个查询的结果 |
UNION | 合并两个查询的结果,并去重 |
EXCEPT | 从左查询结果集中去重出现在右查询中的结果,并去重 |
INTERSECT | 保留左查询结果集中出现在右查询中的结果,并去重 |
以如下两表的数据为例:
create table t1 (c1 int, c2 int);
create table t2 (c1 int, c2 int);
insert into t1 values (1, -1), (2, -2);
insert into t2 values (1, 1), (2, -2), (3, 3);
obclient> SELECT C1, C2 FROM T1 UNION ALL SELECT C1, C2 FROM T2;
+------+------+
| C1 | C2 |
+------+------+
| 1 | -1 |
| 2 | -2 |
| 1 | 1 |
| 2 | -2 |
| 3 | 3 |
+------+------+
5 rows in set (0.01 sec)
obclient> SELECT C1, C2 FROM T1 UNION SELECT C1, C2 FROM T2;
+------+------+
| C1 | C2 |
+------+------+
| 1 | -1 |
| 2 | -2 |
| 1 | 1 |
| 3 | 3 |
+------+------+
4 rows in set (0.01 sec)
obclient> SELECT C1, C2 FROM T1 INTERSECT SELECT C1, C2 FROM T2;
+------+------+
| C1 | C2 |
+------+------+
| 2 | -2 |
+------+------+
1 row in set (0.00 sec)
obclient> SELECT C1, C2 FROM T1 EXCEPT SELECT C1, C2 FROM T2;
+------+------+
| C1 | C2 |
+------+------+
| 1 | -1 |
+------+------+
1 row in set (0.00 sec)
obclient> SELECT C1, C2 FROM T1 UNION SELECT C1, C2 FROM T2 ORDER BY C2 DESC LIMIT 2;
+------+------+
| C1 | C2 |
+------+------+
| 3 | 3 |
| 1 | 1 |
+------+------+
2 rows in set (0.00 sec)
备案信息: 粤ICP备15087711号-2
Copyright © 2008-2024 啊嘎哇在线工具箱 All Rights.
本站所有资料来源于网络,版权归原作者所有,仅作学习交流使用,如不慎侵犯了您的权利,请联系我们。