全部产品

语法

更新时间:2019-07-10 09:24:35

语法

SELECT语句用于从一个或多个表中查询数据,具体语法如下所示。

  1. [ WITH with_query [, ...] ]
  2. SELECT
  3. [ ALL | DISTINCT ] select_expr [, ...]
  4. [ FROM table_reference [, ...] ]
  5. [ WHERE condition ]
  6. [ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ]
  7. [ HAVING condition]
  8. [ WINDOW window_name AS (window_spec) [, window_name AS (window_spec)] ...]
  9. [ { UNION | INTERSECT | EXCEPT } [ ALL | DISTINCT ] select ]
  10. [ ORDER BY {column_name | expr | position} [ASC | DESC], ... [WITH ROLLUP]]
  11. [LIMIT {[offset,] row_count | row_count OFFSET offset}]
  • table_reference:查询的数据源,可以是表、视图、关联表或者子查询。

  • 表名和列名不缺分大小写。

  • 表名和列名中如果含有关键字或者空格等字符,可以使用反引号(``)将其引起来。

WHERE

WHERE关键字后跟BOOLEAN表达式,用于从表中查询满足条件的数据。例如,在CUSTOMER表中查询customer_id2368的顾客信息。

  1. SELECT * FROM CUSTOMER where customer_id=2368;

ALL和DISTINCT

ALL和DISTINCT关键字用于指定查询结果是否返回重复的行,默认值为ALL,即返回所有匹配的行,DISTINCT将从结果集中删除重复的行。

  1. SELECT col1, col2 FROM t1;
  2. SELECT DISTINCT col1, col2 FROM t1;

以下为SELECT中的其他关键字用法。