mysql必知必会 (一)select语句检索数据

文章目录

  • 返回所有行的检索
    • 检索1列
    • 检索多列
    • 检索所有列:*通配符
  • 只返回不同值:distinct
  • 只返回某几行:limit
  • 完全限定表名,列名

返回所有行的检索

检索1列

这里的代码之前在mysql workbench都跑过了,现在在命令行再跑一遍,复习并熟悉mysql命令行

mysql> select prod_name from products;
+---------------------+
| prod_name           |
+---------------------+
| Fish bean bag toy   |
| Bird bean bag toy   |
| Rabbit bean bag toy |
| 8 inch teddy bear   |
| 12 inch teddy bear  |
| 18 inch teddy bear  |
| Raggedy Ann         |
| King doll           |
| Queen doll          |
+---------------------+
9 rows in set (0.01 sec)

没排序,没过滤(返回所有行)
mysql必知必会 (一)select语句检索数据_第1张图片

检索多列

mysql> select prod_id, prod_name, prod_price
    -> from products;
+---------+---------------------+------------+
| prod_id | prod_name           | prod_price |
+---------+---------------------+------------+
| BNBG01  | Fish bean bag toy   |       3.49 |
| BNBG02  | Bird bean bag toy   |       3.49 |
| BNBG03  | Rabbit bean bag toy |       3.49 |
| BR01    | 8 inch teddy bear   |       5.99 |
| BR02    | 12 inch teddy bear  |       8.99 |
| BR03    | 18 inch teddy bear  |      11.99 |
| RGAN01  | Raggedy Ann         |       4.99 |
| RYL01   | King doll           |       9.49 |
| RYL02   | Queen doll          |       9.49 |
+---------+---------------------+------------+
9 rows in set (0.00 sec)

明白为啥mysql命令行要求输入分号后回车才执行命令了,因为sql语句喜欢分行写,如果回车执行就没法弄了。

返回的数据是没有格式的。因为sql语句只负责检索。不过以后也会涉及到用sql语句实现格式化

检索所有列:*通配符

mysql> select * from products;
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
| prod_id | vend_id | prod_name           | prod_price | prod_desc                                                             |
+---------+---------+---------------------+------------+-----------------------------------------------------------------------+
| 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                      |
| 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                         |
| 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语句检索数据_第2张图片在这里插入图片描述

只返回不同值:distinct

返回所有行的一个显著缺点是,如果一列有很多数据是一样,也许需要的只是一共有几个不同的值

如果从供应商表中选择,由于vend_id是主键,所以返回的每一行一定没有重复值

mysql> select vend_id from vendors;
+---------+
| vend_id |
+---------+
| BRE02   |
| BRS01   |
| DLL01   |
| FNG01   |
| FRB01   |
| JTS01   |
+---------+
6 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.02 sec)

可以看到,由于多个产品来自同一个供应商,所以存在重复值,我们只想要唯一值

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

mysql必知必会 (一)select语句检索数据_第3张图片
prod_price和vend_id没有都不同的行就会被显示出来,于是得到的结果虽然不一定是所有行,但是两个列都有可能出现重复值了

mysql> select distinct vend_id, prod_price
    -> from products;
+---------+------------+
| vend_id | prod_price |
+---------+------------+
| DLL01   |       3.49 |
| BRS01   |       5.99 |
| BRS01   |       8.99 |
| BRS01   |      11.99 |
| DLL01   |       4.99 |
| FNG01   |       9.49 |
+---------+------------+
6 rows in set (0.00 sec)

重点是记清楚distinct作用于后面的所有列

只返回某几行:limit

返回前6行

mysql> select prod_name from products
    -> limit 6;
+---------------------+
| prod_name           |
+---------------------+
| Fish bean bag toy   |
| Bird bean bag toy   |
| Rabbit bean bag toy |
| 8 inch teddy bear   |
| 12 inch teddy bear  |
| 18 inch teddy bear  |
+---------------------+
6 rows in set (0.00 sec)

返回第6行开始的3行,注意到第6行的6是从0开始编号的,实际返回的是第7行(如果从1编号的话)

这时候limit后面第一个数字是行的起点,表示从第6行开始,而第二个数字才表示几行

mysql> select prod_name
    -> from products
    -> limit 6, 3;
+-------------+
| prod_name   |
+-------------+
| Raggedy Ann |
| King doll   |
| Queen doll  |
+-------------+
3 rows in set (0.00 sec)

也可以用offset关键字,limit后的数字表示最多输出的行数,这里是3行,从第2行(从0编号)开始

mysql> select prod_name
    -> from products
    -> limit 3 offset 2;
+---------------------+
| prod_name           |
+---------------------+
| Rabbit bean bag toy |
| 8 inch teddy bear   |
| 12 inch teddy bear  |
+---------------------+
3 rows in set (0.00 sec)

注意limit 6, 3和limit 6 offset 3的区别:limit 6, 3相当于是limit 3 offset 6
mysql必知必会 (一)select语句检索数据_第4张图片

完全限定表名,列名

mysql> select products.prod_name
    -> from tysql.products;
+---------------------+
| prod_name           |
+---------------------+
| Fish bean bag toy   |
| Bird bean bag toy   |
| Rabbit bean bag toy |
| 8 inch teddy bear   |
| 12 inch teddy bear  |
| 18 inch teddy bear  |
| Raggedy Ann         |
| King doll           |
| Queen doll          |
+---------------------+
9 rows in set (0.00 sec)

你可能感兴趣的:(数据库)