SQL第2课:检索数据(SELECT)

SQL 是一门语言,而不是一个应用程序,也不是一个数据库。

  • 检索单列
  • 检索多列
  • 检索所有列
  • 返回不同的值
  • 代码注释

关键字(keyword):SQL 里有保留字和非保留字之分。根据标准, 保留字是那些真正的关键字;决不能用它们做标识符。非保留字只是在特定的环境里有特殊的含义, 而在其它环境里是可以用做标识符的。大多数非保留字实际上都是 SQL 声明的内建表和函数的名字。 而非保留字的概念实质上只是用来表示在某些场合里,在一个字上附加了一些预先定义的含义。

mysql> select prod_name
    -> FROM Products;
+---------------------+
| prod_name           |
+---------------------+
| 8 inch teddy bear   |
| 12 inch teddy bear  |
| 18 inch teddy bear  |
| Fish bean bag toy   |
| Bird bean bag toy   |
| Rabbit bean bag toy |
| Raggedy Ann         |
| King doll           |
| Queen doll          |
+---------------------+
9 rows in set (0.00 sec)
  • SQL语句以分号(;)结束
  • 不区分大小写
  • 忽略空格

以上语句等同于:

  select prod_name FROM Products;

检索多列,列名以逗号(,)隔开:

mysql> select prod_name,prod_name,prod_price FROM Products;
+---------------------+---------------------+------------+
| prod_name           | prod_name           | prod_price |
+---------------------+---------------------+------------+
| 8 inch teddy bear   | 8 inch teddy bear   |       5.99 |
| 12 inch teddy bear  | 12 inch teddy bear  |       8.99 |
| 18 inch teddy bear  | 18 inch teddy bear  |      11.99 |
| Fish bean bag toy   | Fish bean bag toy   |       3.49 |
| Bird bean bag toy   | Bird bean bag toy   |       3.49 |
| Rabbit bean bag toy | Rabbit bean bag toy |       3.49 |
| Raggedy Ann         | Raggedy Ann         |       4.99 |
| King doll           | King doll           |       9.49 |
| Queen doll          | Queen doll          |       9.49 |
+---------------------+---------------------+------------+
9 rows in set (0.00 sec)

检索所有列:

mysql> select * FROM Products;
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
| prod_id | vend_id | prod_name           | prod_price | prod_desc                                                             |
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
| BR01    | BRS01   | 8 inch teddy bear   |       5.99 | 8 inch teddy bear, comes with cap and jacket                          |
| BR02    | BRS01   | 12 inch teddy bear  |       8.99 | 12 inch teddy bear, comes with cap and jacket                         |
| BR03    | BRS01   | 18 inch teddy bear  |      11.99 | 18 inch teddy bear, comes with cap and jacket                         |
| BNBG01  | DLL01   | Fish bean bag toy   |       3.49 | Fish bean bag toy, complete with bean bag worms with which to feed it |
| BNBG02  | DLL01   | Bird bean bag toy   |       3.49 | Bird bean bag toy, eggs are not included                              |
| BNBG03  | DLL01   | Rabbit bean bag toy |       3.49 | Rabbit bean bag toy, comes with bean bag carrots                      |
| RGAN01  | DLL01   | Raggedy Ann         |       4.99 | 18 inch Raggedy Ann doll                                              |
| RYL01   | FNG01   | King doll           |       9.49 | 12 inch king doll with royal garments and crown                       |
| RYL02   | FNG01   | Queen doll          |       9.49 | 12 inch queen doll with royal garments and crown                      |
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
9 rows in set (0.00 sec)

检索 不同的值,即重复的不显示:

mysql> select distinct vend_id from Products;
+---------+
| vend_id |
+---------+
| BRS01   |
| DLL01   |
| FNG01   |
+---------+
3 rows in set (0.00 sec)

mysql> select vend_id from Products;
+---------+
| vend_id |
+---------+
| BRS01   |
| BRS01   |
| BRS01   |
| DLL01   |
| DLL01   |
| DLL01   |
| DLL01   |
| FNG01   |
| FNG01   |
+---------+
9 rows in set (0.00 sec)

distinct 作用与所有列,而不是紧跟其后的列。

限制结果(如选择前5列)

mysql> select  prod_name FROM Products limit 5;
+--------------------+
| prod_name          |
+--------------------+
| 8 inch teddy bear  |
| 12 inch teddy bear |
| 18 inch teddy bear |
| Fish bean bag toy  |
| Bird bean bag toy  |
+--------------------+
5 rows in set (0.00 sec)

各种数据库这一SQL并不一致,SQLServer ,Access,DB2,MySQL,Oracle等。

显示后面5行:

mysql> SELECT prod_name
    -> FROM Products
    -> limit 5 OFFSET 5;
+---------------------+
| prod_name           |
+---------------------+
| Rabbit bean bag toy |
| Raggedy Ann         |
| King doll           |
| Queen doll          |
+---------------------+
4 rows in set (0.00 sec)

SQL语句是由DBMS处理的指令。语句的注释一般有3中:

    -- ;  #  ; /* */;
mysql>  SELECT prod_name -- xuanzelie
    -> FROM Products # zaibiaozhong
    -> limit 5 OFFSET 5;/*xuanzehang*/
+---------------------+
| prod_name           |
+---------------------+
| Rabbit bean bag toy |
| Raggedy Ann         |
| King doll           |
| Queen doll          |
+---------------------+
4 rows in set (0.00 sec)

你可能感兴趣的:(SQL第2课:检索数据(SELECT))