day01-检索数据

day01-检索数据

select语句

关键字
作为sql组成部分的保留字
关键字不能作为表或列的名字

如果要使用select?想明白二件事
1.想选择什么?
2.从什么地方选择?




检索单个列


select prod_name
from Products;


输出:

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.00 sec)

mysql> 











提示:
1.SQL语句必须以英文分号;结束
2.sql语句不区分大小写
3.但是其中的表名、列名和值可能有所不同
4.使用空格时都被忽略

检索多个列

select prod_id, prod_name, prod_price
from Products;




输出:
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)







检索所有列


select * 
from Products;



输出:
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> 









注意:
1.一般情况,除非你确实需要表中的每一列,否则不要使用* 通配符,虽然他比较省事,不用明确列出所需列,但检索不需要的列通常就会降低速度和应用程序的性能
2.检索未知列时,如果不明确具体列名,所以他可以帮助你找到名字未知的列



检索不同的值

问题?
如果你不希望每个值每次都出现,该怎么办呢?


select vend_id
from Products;


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

mysql> 












如何检索不同值?
用distinct


select distinct vend_id from Products;


原理:此语句会告诉数据库只返回不同也就是具有唯一性的行

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

mysql> 






注意:
1.不能部分使用distinct,作用于所有的列,不仅仅是跟在其后的那一列,如果,你指定select distinct vend_id, prod_price,则9行里的6行都会被检索出来,因为指定的二列组合起来有6个不同的结果,如果你想想看看究竟有什么不同,你可以试一下下面二条语句?

select distinct vend_id,prod_price from Products;
select vend_id, prod_price from Products;

输出:
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)

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

mysql> 


限制结果


问题?
如果你想只返回或者一定数量的行?怎么办?
如果你是SQL server数据库,语句形式如下:
用top

select top 5 prod_name
from Products;


输出:




如果你是DB2数据库,语句形式为如下:
select prod_name from Products fetch first 5 rows only;

原理:在DB2中,fetch first 5 rows only,就是只要前五行




如果你是oracle数据库,语句形式如下:
基于rownum-行计数器来计算行

select prod_name from Products where rownum <=5;



如果你是MySQL,mariaDB、postgresql,语句形式如下:
需要使用limit子句
select prod_name from Products limit 5;


limit 5 表示mysql返回不超过5行的数据,如果要得到后面的5行数据,需要指定从哪儿开始检索的行数呢?

select prod_name from Products limit 5 offset 5;

limit5 offset 5 表示mysql等数据库返回从第五行起的5行数据
第一个数字是检索的行数,第二个数字是从哪儿开始

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

mysql> select prod_name from Products limit 5 offset 5;
+--------------------+
| prod_name          |
+--------------------+
| 18 inch teddy bear |
| Raggedy Ann        |
| King doll          |
| Queen doll         |
+--------------------+
4 rows in set (0.00 sec)

mysql> 







注意:
1.limit指定返回的行数
2.limit带offset指定从哪儿开始
3,上述例子中,由于只要9种产品,所以只返回了4行数据
4.第一个被检索的行是第0行,而不是第1行。因此,limit 1 offset 1 会检索第2行,而不是第1行
5.limit 4 offset 3也可以简化语句为limit3,4,使用这个语法,注意逗号之前的值对应offset,逗号之后的值对应limit(反着的,要注意)
6.并非所有的SQL实现都一样,不能想当然,比如复杂sql语句……





使用注释

select prod_name -- 这是注释
from Products;



输出:
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.00 sec)

mysql> 




也可以多行注释

/* select prod_name,vend_id from Products;*/
select prod_name from Products;



输出:
mysql> /* select prod_name,vend_id from Products;*/
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.00 sec)

mysql> 




小练习

编写 SQL 语句以从 Customers 表中检索所有客户 ID (cust_id)。

SELECT cust_id
FROM Customers;

答案

mysql> SELECT cust_id
    -> FROM Customers;
+------------+
| cust_id    |
+------------+
| 1000000001 |
| 1000000002 |
| 1000000003 |
| 1000000004 |
| 1000000005 |
+------------+
5 rows in set (0.00 sec)

mysql> 

OrderItems 表包含已订购的每个项目(有些项目被多次订购)。编写 SQL 语句以检索已订购的产品 (prod_id) 列表(不是每个订单,只是唯一的产品列表)。这里有一个提示,您最终应该会显示 7 个唯一的行。

SELECT DISTINCT prod_id
FROM OrderItems;

答案

mysql> SELECT DISTINCT prod_id
    -> FROM OrderItems;
+---------+
| prod_id |
+---------+
| BNBG01  |
| BNBG02  |
| BNBG03  |
| BR01    |
| BR02    |
| BR03    |
| RGAN01  |
+---------+
7 rows in set (0.00 sec)

mysql> 

编写一个 SQL 语句来检索 Customers 表中的所有列,并编写一个仅检索客户 ID 的备用 SELECT。使用注释注释掉一个 SELECT,以便能够运行另一个 SELECT。(当然,要测试这两个语句)。

SELECT *
# SELECT cust_id
FROM Customers;


答案

mysql> SELECT *
    -> # SELECT cust_id
    -> FROM Customers;
+------------+---------------+----------------------+-----------+------------+----------+--------------+--------------------+-----------------------+
| cust_id    | cust_name     | cust_address         | cust_city | cust_state | cust_zip | cust_country | cust_contact       | cust_email            |
+------------+---------------+----------------------+-----------+------------+----------+--------------+--------------------+-----------------------+
| 1000000001 | Village Toys  | 200 Maple Lane       | Detroit   | MI         | 44444    | USA          | John Smith         | sales@villagetoys.com |
| 1000000002 | Kids Place    | 333 South Lake Drive | Columbus  | OH         | 43333    | USA          | Michelle Green     | NULL                  |
| 1000000003 | Fun4All       | 1 Sunny Place        | Muncie    | IN         | 42222    | USA          | Jim Jones          | jjones@fun4all.com    |
| 1000000004 | Fun4All       | 829 Riverside Drive  | Phoenix   | AZ         | 88888    | USA          | Denise L. Stephens | dstephens@fun4all.com |
| 1000000005 | The Toy Store | 4545 53rd Street     | Chicago   | IL         | 54545    | USA          | Kim Howard         | NULL                  |
+------------+---------------+----------------------+-----------+------------+----------+--------------+--------------------+-----------------------+
5 rows in set (0.00 sec)

mysql> 

posted @ 2025-06-17 22:44  三思博客  阅读(29)  评论(0)    收藏  举报