📜  H2数据库-选择

📅  最后修改于: 2020-11-28 14:11:47             🧑  作者: Mango


Select命令用于从一个或多个表中获取记录数据。如果我们设计一个选择查询,那么它将以结果表的形式返回数据,称为结果集

句法

SELECT语句的基本语法如下-

SELECT [ TOP term ] [ DISTINCT | ALL ] selectExpression [,...] 
FROM tableExpression [,...] [ WHERE expression ] 
[ GROUP BY expression [,...] ] [ HAVING expression ] 
[ { UNION [ ALL ] | MINUS | EXCEPT | INTERSECT } select ] [ ORDER BY order [,...] ] 
[ [ LIMIT expression ] [ OFFSET expression ] [ SAMPLE_SIZE rowCountInt ] ] 
[ FOR UPDATE ]

要获取所有可用字段,请使用以下语法。

SELECT * FROM table_name;

考虑具有以下记录的CUSTOMER表-

+----+----------+-----+-----------+----------+ 
| ID | NAME     | AGE | ADDRESS   | SALARY   | 
+----+----------+-----+-----------+----------+ 
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 | 
|  2 | Khilan   |  25 | Delhi     |  1500.00 | 
|  3 | kaushik  |  23 | Kota      |  2000.00 | 
|  4 | Chaitali |  25 | Mumbai    |  6500.00 | 
|  5 | Hardik   |  27 | Bhopal    |  8500.00 | 
|  6 | Komal    |  22 | MP        |  4500.00 | 
|  7 | Muffy    |  24 | Indore    | 10000.00 | 
+----+----------+-----+-----------+----------+ 

要获取客户表以及给定的数据,请执行以下查询。

CREATE TABLE CUSTOMER (id number, name varchar(20), age number, address varchar(20), 
salary number);  

INSERT into CUSTOMER values (1, 'Ramesh', 32, 'Ahmedabad', 2000); 
INSERT into CUSTOMER values (2, 'Khilan', 25, 'Delhi', 1500); 
INSERT into CUSTOMER values (3, 'kaushik', 23, 'Kota', 2000); 
INSERT into CUSTOMER values (4, 'Chaitali', 25, 'Mumbai', 6500); 
INSERT into CUSTOMER values (5, 'Hardik', 27, 'Bhopal', 8500); 
INSERT into CUSTOMER values (6, 'Komal', 22, 'MP', 4500); 
INSERT into CUSTOMER values (7, 'Muffy', 24, 'Indore', 10000);

以下命令是一个示例,它将获取CUSTOMER表中可用的客户的ID,名称和薪水字段。

SELECT ID, NAME, SALARY FROM CUSTOMERS;

上面的命令产生以下结果。

+----+----------+----------+ 
| ID | NAME     | SALARY   | 
+----+----------+----------+ 
|  1 | Ramesh   |  2000.00 | 
|  2 | Khilan   |  1500.00 | 
|  3 | kaushik  |  2000.00 | 
|  4 | Chaitali |  6500.00 | 
|  5 | Hardik   |  8500.00 | 
|  6 | Komal    |  4500.00 | 
|  7 | Muffy    | 10000.00 | 
+----+----------+----------+ 

使用以下查询来获取CUSTOMERS表的所有字段。

SQL> SELECT * FROM CUSTOMERS;

上面的查询产生以下结果-

+----+----------+-----+-----------+----------+ 
| ID | NAME     | AGE | ADDRESS   | SALARY   | 
+----+----------+-----+-----------+----------+ 
|  1 | Ramesh   |  32 | Ahmedabad |  2000.00 | 
|  2 | Khilan   |  25 | Delhi     |  1500.00 | 
|  3 | kaushik  |  23 | Kota      |  2000.00 | 
|  4 | Chaitali |  25 | Mumbai    |  6500.00 | 
|  5 | Hardik   |  27 | Bhopal    |  8500.00 | 
|  6 | Komal    |  22 | MP        |  4500.00 | 
|  7 | Muffy    |  24 | Indore    | 10000.00 | 
+----+----------+-----+-----------+----------+