目录
四、检索数据
检索单个列
检索多个列
检索所有列
检索不同的行 DISTINCT
限制结果 LIMIT
使用完全限定的表名
五、排序检索数据
排序数据 ORDER BY
按多个列排序
指定排序方向 DESC(降序)、ASC(升序)
使用SELECT检索表数据,必须知道给出两条信息——想选择什么(列),从什么地方选择(表)
SELECT `prod_name` FROM `products`;
mysql> SELECT `prod_name` FROM `products`;
+----------------+
| prod_name |
+----------------+
| .5 ton anvil |
| 1 ton anvil |
| 2 ton anvil |
| Detonator |
| Bird seed |
| Carrots |
| Fuses |
| JetPack 1000 |
| JetPack 2000 |
| Oil can |
| Safe |
| Sling |
| TNT (1 stick) |
| TNT (5 sticks) |
+----------------+
14 rows in set (0.00 sec)
ps. 如果不明确排序查询结果,则返回的数据的顺序没有特殊意义。可能是添加时的顺序,也可能不是。
另外,SQL语句不区分大小写,但是一般SQL关键字大写,列和表名小写,便于阅读和调试。
SQL也不在意空格,但是应该使用空格或多行以便于阅读调试。
mysql> SELECT `prod_name`, `prod_name`, `prod_price` FROM `products`;
+----------------+----------------+------------+
| prod_name | prod_name | prod_price |
+----------------+----------------+------------+
| .5 ton anvil | .5 ton anvil | 5.99 |
| 1 ton anvil | 1 ton anvil | 9.99 |
| 2 ton anvil | 2 ton anvil | 14.99 |
| Detonator | Detonator | 13.00 |
| Bird seed | Bird seed | 10.00 |
| Carrots | Carrots | 2.50 |
| Fuses | Fuses | 3.42 |
| JetPack 1000 | JetPack 1000 | 35.00 |
| JetPack 2000 | JetPack 2000 | 55.00 |
| Oil can | Oil can | 8.99 |
| Safe | Safe | 50.00 |
| Sling | Sling | 4.49 |
| TNT (1 stick) | TNT (1 stick) | 2.50 |
| TNT (5 sticks) | TNT (5 sticks) | 10.00 |
+----------------+----------------+------------+
14 rows in set (0.00 sec)
mysql> SELECT * FROM `products`;
+---------+---------+----------------+------------+----------------------------------------------------------------+
| prod_id | vend_id | prod_name | prod_price | prod_desc |
+---------+---------+----------------+------------+----------------------------------------------------------------+
| ANV01 | 1001 | .5 ton anvil | 5.99 | .5 ton anvil, black, complete with handy hook |
| ANV02 | 1001 | 1 ton anvil | 9.99 | 1 ton anvil, black, complete with handy hook and carrying case |
| ANV03 | 1001 | 2 ton anvil | 14.99 | 2 ton anvil, black, complete with handy hook and carrying case |
| DTNTR | 1003 | Detonator | 13.00 | Detonator (plunger powered), fuses not included |
| FB | 1003 | Bird seed | 10.00 | Large bag (suitable for road runners) |
| FC | 1003 | Carrots | 2.50 | Carrots (rabbit hunting season only) |
| FU1 | 1002 | Fuses | 3.42 | 1 dozen, extra long |
| JP1000 | 1005 | JetPack 1000 | 35.00 | JetPack 1000, intended for single use |
| JP2000 | 1005 | JetPack 2000 | 55.00 | JetPack 2000, multi-use |
| OL1 | 1002 | Oil can | 8.99 | Oil can, red |
| SAFE | 1003 | Safe | 50.00 | Safe with combination lock |
| SLING | 1003 | Sling | 4.49 | Sling, one size fits all |
| TNT1 | 1003 | TNT (1 stick) | 2.50 | TNT, red, single stick |
| TNT2 | 1003 | TNT (5 sticks) | 10.00 | TNT, red, pack of 10 sticks |
+---------+---------+----------------+------------+----------------------------------------------------------------+
14 rows in set (0.00 sec)
ps. 除了检索未知列的情况,或者确实需要每个列,否则最好别用 * 通配符(影响性能)
mysql> SELECT `vend_id` FROM `products`;
+---------+
| vend_id |
+---------+
| 1001 |
| 1001 |
| 1001 |
| 1002 |
| 1002 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1003 |
| 1005 |
| 1005 |
+---------+
14 rows in set (0.00 sec)
mysql> SELECT DISTINCT `vend_id` FROM `products`;
+---------+
| vend_id |
+---------+
| 1001 |
| 1002 |
| 1003 |
| 1005 |
+---------+
4 rows in set (0.00 sec)
ps. 不能部分使用DOSTINCT。该关键字应用于所有的列,而不仅仅是前置它的列。select多个列则选出组合不同的列。
mysql> SELECT DISTINCT `vend_id`, `prod_price` FROM `products`;
+---------+------------+
| vend_id | prod_price |
+---------+------------+
| 1001 | 5.99 |
| 1001 | 9.99 |
| 1001 | 14.99 |
| 1003 | 13.00 |
| 1003 | 10.00 |
| 1003 | 2.50 |
| 1002 | 3.42 |
| 1005 | 35.00 |
| 1005 | 55.00 |
| 1002 | 8.99 |
| 1003 | 50.00 |
| 1003 | 4.49 |
+---------+------------+
12 rows in set (0.00 sec)
LIMIT 5; 返回结果不多于5行
LIMIT 5, 5; 第一个数为开始的位置,第二个数为要检索的行数。(只有一个值的时候默认从0行开始)
LIMIT 4 OFFSET 3; MySQL5的新写法,从行3开始取4行。= LIMIT 3, 4;
mysql> SELECT `prod_name`
-> FROM `products`
-> LIMIT 5;
+--------------+
| prod_name |
+--------------+
| .5 ton anvil |
| 1 ton anvil |
| 2 ton anvil |
| Detonator |
| Bird seed |
+--------------+
5 rows in set (0.00 sec)
mysql> SELECT `prod_name`
-> FROM `products`
-> LIMIT 5,5;
+--------------+
| prod_name |
+--------------+
| Carrots |
| Fuses |
| JetPack 1000 |
| JetPack 2000 |
| Oil can |
+--------------+
5 rows in set (0.00 sec)
1.FROM后的表名加上指定数据库
2.SELECT后的列指定表名
SELECT `products`.`prod_name`
FROM `crashcourse`.`products`;
一般检索出来的数据会按照添加到表中的顺序来显示,但是当进行过更新或删除,数据会受到MySQL重用回收存储空间的影响。所以不应该依赖于这个默认的顺序。
mysql> SELECT `prod_name`
-> FROM `products`
-> ORDER BY `prod_name`;
+----------------+
| prod_name |
+----------------+
| .5 ton anvil |
| 1 ton anvil |
| 2 ton anvil |
| Bird seed |
| Carrots |
| Detonator |
| Fuses |
| JetPack 1000 |
| JetPack 2000 |
| Oil can |
| Safe |
| Sling |
| TNT (1 stick) |
| TNT (5 sticks) |
+----------------+
14 rows in set (0.00 sec)
ps. 一般排序基准列都是被选择的、但可以使用非检索列作为排序的基准列。
mysql> SELECT `prod_id`, `prod_price`, `prod_name`
-> FROM `products`
-> ORDER BY `prod_price`,`prod_name`;
+---------+------------+----------------+
| prod_id | prod_price | prod_name |
+---------+------------+----------------+
| FC | 2.50 | Carrots |
| TNT1 | 2.50 | TNT (1 stick) |
| FU1 | 3.42 | Fuses |
| SLING | 4.49 | Sling |
| ANV01 | 5.99 | .5 ton anvil |
| OL1 | 8.99 | Oil can |
| ANV02 | 9.99 | 1 ton anvil |
| FB | 10.00 | Bird seed |
| TNT2 | 10.00 | TNT (5 sticks) |
| DTNTR | 13.00 | Detonator |
| ANV03 | 14.99 | 2 ton anvil |
| JP1000 | 35.00 | JetPack 1000 |
| SAFE | 50.00 | Safe |
| JP2000 | 55.00 | JetPack 2000 |
+---------+------------+----------------+
14 rows in set (0.01 sec)
ps. 首先按第一个字段排序,在第一个字段值相同的情况下,按下一个字段排序。如果第一个字段的值都不相同,第二字段的存在没有意义。
mysql> SELECT `prod_id`, `prod_price`, `prod_name`
-> FROM `products`
-> ORDER BY `prod_price` DESC,`prod_name`;
+---------+------------+----------------+
| prod_id | prod_price | prod_name |
+---------+------------+----------------+
| JP2000 | 55.00 | JetPack 2000 |
| SAFE | 50.00 | Safe |
| JP1000 | 35.00 | JetPack 1000 |
| ANV03 | 14.99 | 2 ton anvil |
| DTNTR | 13.00 | Detonator |
| FB | 10.00 | Bird seed |
| TNT2 | 10.00 | TNT (5 sticks) |
| ANV02 | 9.99 | 1 ton anvil |
| OL1 | 8.99 | Oil can |
| ANV01 | 5.99 | .5 ton anvil |
| SLING | 4.49 | Sling |
| FU1 | 3.42 | Fuses |
| FC | 2.50 | Carrots |
| TNT1 | 2.50 | TNT (1 stick) |
+---------+------------+----------------+
14 rows in set (0.00 sec)
ps.
默认就是升序,所以不用写ASC
DESC关键字只作用于其前面那一个列。
ORDER BY 结合 LIMIT 可以得到某列中最低/最高值,但是ORDER BY要在FROM之后,LIMIT要在ORDER BY之后。
mysql> SELECT `prod_id`, `prod_price`, `prod_name`
-> FROM `products`
-> ORDER BY `prod_price` DESC
-> LIMIT 1;
+---------+------------+--------------+
| prod_id | prod_price | prod_name |
+---------+------------+--------------+
| JP2000 | 55.00 | JetPack 2000 |
+---------+------------+--------------+
1 row in set (0.00 sec)
mysql> SELECT `prod_id`, `prod_price`, `prod_name`
-> FROM `products`
-> LIMIT 1
-> ORDER BY `prod_price` DESC;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY `prod_price` DESC' at line 4
mysql>