SELECT

功能描述

SELECT用于从表或视图中取出数据。

SELECT语句就像叠加在数据库表上的过滤器,利用SQL关键字从数据表中过滤出用户需要的数据。

注意事项

  • 本章节只包含shark新增的语法,原openGauss的语法未做删除和修改。原openGauss的SELECT语法请参考章节SELECT。
  • 新增支持TOP子句。
  • 新增支持table_hint子句。

语法格式

  • 查询数据
1. [ WITH [ RECURSIVE ] with_query [, ...] ]
2. SELECT [/*+ plan_hint */] [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ]
3. [ top_clause ]
4. { * | {expression [ [ AS ] output_name ]} [, ...] }
5. [ into_option ]
6. [ FROM from_item [, ...] ]
7. [ WHERE condition ]
8. [ [ START WITH condition ] CONNECT BY [NOCYCLE] condition [ ORDER SIBLINGS BY expression ] ]
9. [ GROUP BY grouping_element [, ...] ]
10. [ HAVING condition [, ...] ]
11. [ WINDOW {window_name AS ( window_definition )} [, ...] ]
12. [ { UNION | INTERSECT | EXCEPT | MINUS } [ ALL | DISTINCT ] select ]
13. [ ORDER BY {expression [ [ ASC | DESC | USING operator ] | nlssort_expression_clause ] [ NULLS { FIRST | LAST } ]} [, ...] ]
14. [ LIMIT { [offset,] count | ALL } ]
15. [ OFFSET start [ ROW | ROWS ] ]
16. [ FETCH { FIRST | NEXT } [ count ] [PERCENT] { ROW | ROWS } { ONLY | WITH TIES } ]
17. [ into_option ]
18. [ {FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF table_name [, ...] ] [ NOWAIT | WAIT N]} [...] ]
19. [ into_option ];
  • 其中TOP子句top_clause为:

1. TOP (expression) [ PERCENT ] [ WITH TIES ] - 其中table_hint子句table_hint_clause为:

1. [ WITH ] ( <table_hint> [, ...] )

参数说明

  • TOP (expression) [ PERCENT ] [ WITH TIES ]

TOP子句限制查询结果集中返回指定的行数或行数的百分比。将 TOP 子句与 ORDER BY 子句一起使用时,结果集被限制为有序的指定行数,否则TOP子句按未定义的顺序返回指定行数。使用PERCENT关键字可以指定返回的行数为查询结果集的百分比。WITH TIES关键字表示返回指定的行数以及结果集有序情况下所有与最后一行相同的值。

SELECT - 图1 说明:

  • 兼容D库中,使用PERCENT关键字时,PERCENT值的范围为[0,100],超范围报错。
  • 兼容D库中,指定的百分比数和结果集总数相乘后不为整数时,向上取整到最接近的整数值。指定的行数不为整数时,四舍五入到最接近的整数值。
  • 兼容D库中,WITH TIES必须和ORDER BY 子句一起使用,否则报错。这一规则适用于TOP子句和FETCH子句。
  • 兼容D库中,TOP子句和LIMIT子句以及FETCH子句不能同时使用。
  • [ WITH ] ( <table_hint> [, …] )
  • 针对SELECT子句,在未给出WITH时,table_hint仅支持给出一个hint,在给出WITH时,table_hint支持给出一个列表选项,列表通过逗号或者空格分隔,即(hint1)、WITH (hint1)、WITH (hint1, hint2, …)、WITH (hint1 hint2 …)均支持。
  • 支持的hint包括NOLOCK、READUNCOMMITTED、UPDLOCK、REPEATABLEREAD、SERIALIZABLE、READCOMMITTED、TABLOCK、TABLOCKX、PAGLOCK、ROWLOCK、NOWAIT、READPAST、XLOCK、SNAPSHOT、NOEXPAND。
  • 当上述hint需要当做标识符,用于列名、变量名等,需要设置d_format_behavior_compat_options = ‘enable_table_hint_identifier’,该变量默认值d_format_behavior_compat_options = ‘’。
  • 所有的hint仅语法支持,无实际含义。
  • table_hint子句位于from_item子句中。

1. {[ ONLY ] table_name [ * ] [ partition_clause ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ] 2. [ TABLESAMPLE sampling_method ( argument [, ...] ) [ REPEATABLE ( seed ) ] ] [table_hint_clause] 3. [TIMECAPSULE {TIMESTAMP|CSN} expression] 4. |( select ) [ AS ] alias [ ( column_alias [, ...] ) ] 5. |with_query_name [ [ AS ] alias [ ( column_alias [, ...] ) ] ] 6. |function_name ( [ argument [, ...] ] ) [ AS ] alias [ ( column_alias [, ...] | column_definition [, ...] ) ] 7. |function_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] ) 8. |from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) ] 9. |rotate_clause 10. |notrotate_clause 11. |lateral lateral_subquery [ AS ] alias 12. |from_item cross apply lateral_subquery [ AS ] alias 13. |from_item outer apply lateral_subquery [ AS ] alias}

  • 对于JOIN场景,每个表均支持单独给出hint。
  • SELECT INTO场景支持相关table_hint语法。
  • 针对hint,会打印相关NOTICE信息。

TOP子句示例

1. opengauss=# CREATE TABLE Products(QtyAvailable smallint, UnitPrice money, InventoryValue AS (QtyAvailable * UnitPrice) PERSISTED);
2. CREATE TABLE
3. opengauss=# INSERT INTO Products(QtyAvailable, UnitPrice) VALUES (25, 2.00), (10, 1.5), (25, 2.00), (10, 1.5), (10, 1.5);
4. INSERT 0 5
5. opengauss=# select * from Products ;
6. qtyavailable | unitprice | inventoryvalue
7. --------------+-----------+----------------
8. 25 |     $2.00 |         $50.00
9. 10 |     $1.50 |         $15.00
10. 25 |     $2.00 |         $50.00
11. 10 |     $1.50 |         $15.00
12. 10 |     $1.50 |         $15.00
13. (5 rows)

15. opengauss=# select TOP 4 * from Products ORDER BY qtyavailable;
16. qtyavailable | unitprice | inventoryvalue
17. --------------+-----------+----------------
18. 10 |     $1.50 |         $15.00
19. 10 |     $1.50 |         $15.00
20. 10 |     $1.50 |         $15.00
21. 25 |     $2.00 |         $50.00
22. (4 rows)

24. opengauss=# select TOP 2 PERCENT * from Products ORDER BY qtyavailable;
25. qtyavailable | unitprice | inventoryvalue
26. --------------+-----------+----------------
27. 10 |     $1.50 |         $15.00
28. (1 row)

30. opengauss=# select TOP 2 PERCENT WITH TIES * from Products ORDER BY qtyavailable;
31. qtyavailable | unitprice | inventoryvalue
32. --------------+-----------+----------------
33. 10 |     $1.50 |         $15.00
34. 10 |     $1.50 |         $15.00
35. 10 |     $1.50 |         $15.00
36. (3 rows)

table_hint子句示例

1. create table test_hint(id int);

3. select * from test_hint t (nolock);
4. NOTICE:  The nolock option is currently ignored
5. id
6. ----
7. (0 rows)

9. select * from test_hint (readuncommitted);
10. NOTICE:  The readuncommitted option is currently ignored
11. id
12. ----
13. (0 rows)

15. select * from test_hint t with (nolock);
16. NOTICE:  The nolock option is currently ignored
17. id
18. ----
19. (0 rows)

21. select * from test_hint t with (nolock, nowait);
22. NOTICE:  The nolock option is currently ignored
23. NOTICE:  The nowait option is currently ignored
24. id
25. ----
26. (0 rows)

28. select * from test_hint with (nolock nowait);
29. NOTICE:  The nolock option is currently ignored
30. NOTICE:  The nowait option is currently ignored
31. id
32. ----
33. (0 rows)

35. --join
36. create table t1(col1 int, col2 int, col3 int, col4 int, col5 int);
37. create table t2(col1 int, col2 int, col3 int, col4 int, col5 int);

39. select * from t1 a (nolock) left join t2 b (nolock) on a.col1 = b.col1;
40. NOTICE:  The nolock option is currently ignored
41. NOTICE:  The nolock option is currently ignored
42. col1 | col2 | col3 | col4 | col5 | col1 | col2 | col3 | col4 | col5
43. ------+------+------+------+------+------+------+------+------+------
44. (0 rows)

46. select * from t1 with (nolock) left join t2 with (nolock) on t1.col1 = t2.col1;
47. NOTICE:  The nolock option is currently ignored
48. NOTICE:  The nolock option is currently ignored
49. col1 | col2 | col3 | col4 | col5 | col1 | col2 | col3 | col4 | col5
50. ------+------+------+------+------+------+------+------+------+------
51. (0 rows)

53. select * from t1 a full join t2 b (nolock) on a.col4 = b.col4 where a.col1 > 10 and b.col4 < 100;
54. NOTICE:  The nolock option is currently ignored
55. col1 | col2 | col3 | col4 | col5 | col1 | col2 | col3 | col4 | col5
56. ------+------+------+------+------+------+------+------+------+------
57. (0 rows)

59. -- select into
60. create table t3(col1 int, col2 int, col3 int, col4 int, col5 int);
61. create table t4(c1 int, c2 int, c3 int, c4 int, c5 int);

63. select * into test1 from t3 with (nolock, nowait) where col1 > 10;
64. NOTICE:  The nolock option is currently ignored
65. NOTICE:  The nowait option is currently ignored

67. select * into table test2 from t3 with (nolock, nowait) where col1 > 10;
68. NOTICE:  The nolock option is currently ignored
69. NOTICE:  The nowait option is currently ignored

71. select * into test3 from t3 with (nolock, nowait) cross join t4 with (nolock, nowait);
72. NOTICE:  The nolock option is currently ignored
73. NOTICE:  The nowait option is currently ignored
74. NOTICE:  The nolock option is currently ignored
75. NOTICE:  The nowait option is currently ignored

相关链接

SELECT