0
点赞
收藏
分享

微信扫一扫

4.检索数据

八卦城的酒 2022-07-12 阅读 23

学习目标

  1. 使用select语句从表中检索一个或多个数据列
  2. 学会使用distinct关键字
  3. 学会使用limit关键字
  4. 学会使用完全限定的表名
  5. 总结以上知识点

一.select语句

select语句最常用,用途是从一个或多个表中检索信息(查询数据)。 select检索数据,必须至少给出俩条信息

  1. 想选择什么
  2. 以及从什么地方选择

二.检索单个列

1.查询顾客表中顾客的名字:

select cust_name from customers; 格式:select 列名 from 表名; 含义:利用select语句从customers表中检索一个名为cust_name的列。

mysql> select cust_name from customers;
+----------------+
| cust_name |
+----------------+
| Coyote Inc. |
| Mouse House |
| Wascals |
| Yosemite Place |
| E Fudd |
+----------------+
5 rows in set (0.00 sec)

上述:一条简单的select语句将返回表中所有行。 注:

  1. 多条语句必须以分号(;)分隔。
  2. SQL语句不分大小写,一般关键字用大写,列和表名用小写,易于阅读和调试。
  3. MySQL4.1及之前的版本,标识符默认是区分大小写的。
  4. SQL语句单词间要有空格,SQL语句可以在一行给出,也可以分成多行(易阅读和调试)。

三.检索多个列

1.查询供应商表中供应商id,供应商name,供应商country:

select vend_id,vend_name,vend_country from vendors; 格式:select 列名1,列名2,列名3 from 表名; 注:列名之间用逗号分隔。

mysql> select vend_id,vend_name,vend_country from vendors;
+---------+----------------+--------------+
| vend_id | vend_name | vend_country |
+---------+----------------+--------------+
| 1001 | Anvils R Us | USA |
| 1002 | LT Supplies | USA |
| 1003 | ACME | USA |
| 1004 | Furball Inc. | USA |
| 1005 | Jet Set | England |
| 1006 | Jouets Et Ours | France |
+---------+----------------+--------------+
6 rows in set (0.00 sec)

注:

  1. select语句一般返回原始的,无格式的数据。
  2. 数据的格式化是一个表示问题,而不是一个检索问题。

四.检索所有列

1.查询订单表中所有列:

select * from orders; 格式:select * from 表名; 如果给定一个通配符(*),则返回表中的所有列。 表的模式变化(如添加或删除列)可能会导致顺序的变化。 注:

  1. 除非你确实需要表中的每个列,否则最好别使用通配符(*)。
  2. 缺点:特别在海量数据下,使用通配符(*)通常会降低检索查询效率和应用程序的性能。
  3. 优点:由于不明确指定列名,所以能检索出名字未知的列。

mysql> select * from orders;
+-----------+---------------------+---------+
| order_num | order_date | cust_id |
+-----------+---------------------+---------+
| 20005 | 2005-09-01 00:00:00 | 10001 |
| 20006 | 2005-09-12 00:00:00 | 10003 |
| 20007 | 2005-09-30 00:00:00 | 10004 |
| 20008 | 2005-10-03 00:00:00 | 10005 |
| 20009 | 2005-10-08 00:00:00 | 10001 |
+-----------+---------------------+---------+
5 rows in set (0.00 sec)

五.检索不同的行(distinct)

1.查询产品表中产品供应商ID(去重):

select distinct vend_id from products; 格式:select distinct 列名 from 表名; distinct含义:返回不同(唯一)vend_id行

mysql> select distinct vend_id from products;
+---------+
| vend_id |
+---------+
| 1001 |
| 1002 |
| 1003 |
| 1005 |
+---------+
4 rows in set (0.00 sec)

1. 不加distinct,共14行
2. 加distinct,共4行

注:如果使用distinct关键字,它必须直接放在列名的前面。distinct关键字应用于所有列,而不是前置它的列。 select distinct vend_id,prod_price ,除非指定的俩个列都不同,否则所有的行都将被检索出来。

六.限制结果(limit)

为了返回第一行或前几行,可以使用limit子句

1.查询产品表中prod_name,prod_id,prod_price的前5行记录

select prod_name,prod_id,prod_price from products limit 5; 格式:select 列名1,列名2,列名3 from 表名 limit 数字;

mysql> select prod_name,prod_id,prod_price from products limit 5;
+--------------+---------+------------+
| prod_name | prod_id | prod_price |
+--------------+---------+------------+
| .5 ton anvil | ANV01 | 5.99 |
| 1 ton anvil | ANV02 | 9.99 |
| 2 ton anvil | ANV03 | 14.99 |
| Detonator | DTNTR | 13.00 |
| Bird seed | FB | 10.00 |
+--------------+---------+------------+
5 rows in set (0.00 sec)

2.查询产品表中prod_name,prod_id,prod_price的从第6行记录开始的5行

select prod_name,prod_id,prod_price from products limit 5,5; 格式:select 列名1,列名2,列名3 from 表名 limit m,n; **注:**从0开始,表示第一条记录。 其中m是指记录开始的index,n是指从第m+1条开始,取n条。

mysql> select prod_name,prod_id,prod_price from products limit 5,5;
+--------------+---------+------------+
| prod_name | prod_id | prod_price |
+--------------+---------+------------+
| Carrots | FC | 2.50 |
| Fuses | FU1 | 3.42 |
| JetPack 1000 | JP1000 | 35.00 |
| JetPack 2000 | JP2000 | 55.00 |
| Oil can | OL1 | 8.99 |
+--------------+---------+------------+
5 rows in set (0.00 sec)

demo: select * from tablename limit 2,4 即取出第3条至第6条,4条记录

3.行数不够时(返回实际行数)

查询产品表中prod_name,prod_id,prod_price的第11行-15行记录 select prod_name,prod_id,prod_price from products limit 10,5; 格式:select 列名1,列名2,列名3 from 表名 limit m,n;

mysql> select prod_name,prod_id,prod_price from products limit 10,5;
+----------------+---------+------------+
| prod_name | prod_id | prod_price |
+----------------+---------+------------+
| Safe | SAFE | 50.00 |
| Sling | SLING | 4.49 |
| TNT (1 stick) | TNT1 | 2.50 |
| TNT (5 sticks) | TNT2 | 10.00 |
+----------------+---------+------------+
4 rows in set (0.00 sec)

4.limit m offset n

查询产品表中prod_name,prod_id,prod_price从第4行开始,取5条 select prod_name,prod_id,prod_price from products limit 5 offset 3; 格式:select 列名1,列名2,列名3 from 表名 limit m offset n; **注:**limit m offset n 从n行取m条记录

mysql> select prod_name,prod_id,prod_price from products limit 5 offset 3;
+--------------+---------+------------+
| prod_name | prod_id | prod_price |
+--------------+---------+------------+
| Detonator | DTNTR | 13.00 |
| Bird seed | FB | 10.00 |
| Carrots | FC | 2.50 |
| Fuses | FU1 | 3.42 |
| JetPack 1000 | JP1000 | 35.00 |
+--------------+---------+------------+
5 rows in set (0.00 sec)

七.使用完全限定名

1.查询订单中实际物品表中的物品订单号,物品价格

select orderitems.order_num,orderitems.item_price from crashcourse.orderitems; 格式:select 表名.列名1,表名.列名2 from 数据库.表名 ;

mysql> select orderitems.order_num,orderitems.item_price from crashcourse.orderitems;
+-----------+------------+
| order_num | item_price |
+-----------+------------+
| 20005 | 5.99 |
| 20005 | 9.99 |
| 20005 | 10.00 |
| 20005 | 10.00 |
| 20006 | 55.00 |
| 20007 | 10.00 |
| 20008 | 2.50 |
| 20009 | 10.00 |
| 20009 | 8.99 |
| 20009 | 4.49 |
| 20009 | 14.99 |
+-----------+------------+
11 rows in set (0.00 sec)

八.总结

学习了:

  1. select语句
  2. 检索单个列
  3. 检索多个列
  4. distinct去重
  5. limit m,n
  6. 使用完全限定名

多思考,多总结,多输出,一键四连~

但行好事,莫问前程,我们下篇见~

往期文章:

​​3.使用MySQL​​

​​2.MySQL简介​​

​​1.了解MySQL​​

举报

相关推荐

4. MongoDB数据库

4. AQS

4.线程

4. Hystrix

0 条评论