SELECT
语句用于从一个或多个表中查询数据,并根据需要进行过滤、分组、排序等操作。
[ WITH with_query [, ...] ]
SELECT
[ ALL | DISTINCT ] select_expr [, ...]
[ FROM table_reference [, ...] ]
[ WHERE condition ]
[ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ]
[ HAVING condition]
[ WINDOW window_name AS (window_spec) [, window_name AS (window_spec)] ...]
[ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
[ ORDER BY {column_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
table_reference
:查询的数据源,可以是表、视图、关联表或者子查询。表名和列名不区分大小写。
表名和列名中如果含有关键字或者空格等字符,可以使用反引号(``)将其引起来。
WHERE
WHERE
关键字后跟BOOLEAN
表达式,用于从表中查询满足条件的数据。例如,在CUSTOMER表中查询customer_id
为2368
的顾客信息。
SELECT * FROM CUSTOMER where customer_id=2368;
ALL和DISTINCT
ALL和DISTINCT关键字用于指定查询结果是否返回重复的行,默认值为ALL,即返回所有匹配的行,DISTINCT将从结果集中删除重复的行。
SELECT id FROM orders;
SELECT DISTINCT id FROM orders;
以下为SELECT中的其他关键字用法。