更新时间:2020-08-17 16:08
SELECT 用于从一个或多个表中查询数据。
SELECT
[ALL | DISTINCT]
select_expr [, select_expr ...]
[FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | expr | position}
[HAVING where_condition]
[ORDER BY {col_name | expr | position}
[ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
[FOR UPDATE]
SELECT 子句说明:
以下是使用 SELECT 时应注意的一些事项:
应改成如下 SQL:
SELECT col_name FROM tbl_name HAVING col_name > 0;
SELECT col_name FROM tbl_name WHERE col_name > 0;
SELECT user, MAX(salary) FROM users
GROUP BY user HAVING MAX(salary) > 10;
SELECT user FROM users GROUP BY age ORDER BY salary;
table_references:
escaped_table_reference [, escaped_table_reference] ...
escaped_table_reference:
table_reference
| { OJ table_reference }
table_reference:
table_factor
| join_table
table_factor:
[schema_name.]tbl_name [[AS] alias] [index_hint_list]
| table_subquery [AS] alias
| ( table_references )
join_table:
table_reference [INNER | CROSS] JOIN table_factor [join_condition]
| table_reference {LEFT|RIGHT} [OUTER] JOIN table_reference join_condition
join_condition:
ON conditional_expr
| USING (column_list)
index_hint_list:
index_hint [, index_hint] ...
index_hint:
USE {INDEX|KEY}
[FOR {JOIN|ORDER BY|GROUP BY}] ([index_list])
| IGNORE {INDEX|KEY}
[FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
| FORCE {INDEX|KEY}
[FOR {JOIN|ORDER BY|GROUP BY}] (index_list)
index_list:
index_name [, index_name] ...
使用 JOIN 语句时,应考虑如下因素:
SELECT * FROM t1 INNER JOIN t2 WHERE t1.id > 10
SELECT * FROM t1, t2 WHERE t1.id > 10
USING(column_list)
指定连接两表中都存在的列名,PolarDB-X 会按照这些列构建等值条件。如下两个 SQL 片段等价:
a LEFT JOIN b USING(c1, c2)
a LEFT JOIN b ON a.c1 = b.c1 AND a.c2 = b.c2
t1, t2 JOIN t3
会转换为(t1, (t2 JOIN t3)
,而不是((t1, t2) JOIN t3)
。PolarDB-X 支持如下 UNION 语法:
SELECT ...
UNION [ALL | DISTINCT] SELECT ...
[UNION [ALL | DISTINCT] SELECT ...]
注意:对于 UNION 中的每个 SELECT, PolarDB-X 暂不支持使用多个同名的列,如下:
# 如下 SQL 的 SELECT 中存在重复的列名,暂不支持
SELECT id, id, name FROM t1 UNION SELECT pk, pk, name FROM t2;
在文档使用中是否遇到以下问题
更多建议
匿名提交