SELECT查询

SELECT用于从一个或多个表中检索信息,使用SELECT检索数据表需要明确:

想选择什么,以及从什么地方选择

1.检索单个列、多个列、所有列

检索单个列的sql语法:

SELECT 列名 FROM 表名

示例:

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)

检索多个列的SQL语法:

SELECT  列名1,列名2,...列名n   FROM  表名

示例:

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 |
+---------+---------------------+------------+

检索所有列使用*通配符:

SELECT * FROM 表名;

示例:

mysql> SELECT * FROM Orders; +-----------+---------------------+------------+
| order_num | order_date | cust_id | +-----------+---------------------+------------+
|     20005 | 2004-05-01 00:00:00 | 1000000001 |
|     20006 | 2004-01-12 00:00:00 | 1000000003 |
|     20007 | 2004-01-30 00:00:00 | 1000000004 |
|     20008 | 2004-02-03 00:00:00 | 1000000005 |
| 20009 | 2004-02-08 00:00:00 | 1000000001 | +-----------+---------------------+------------+
5 rows in set (0.01 sec)

2.使用DISTINCT关键字

如果想查询某一列不重复的结果,可以使用DISTINCT关键字去除重复项返回不重复的结果。从Products表中查询不重复的供应商ID,返回所有结果的查询:

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

使用DISTINCT关键字去除重复项:

mysql> SELECT DISTINCT vend_id FROM Products; +---------+
| vend_id | +---------+
| DLL01   |
| BRS01   |
| FNG01 | +---------+
3 rows in set (0.00 sec)

3.指定查询数量

SELECT列名的方式会返回所有匹配的行,如果想返回一定数量的记录可以在SELECT查询后面加上限制。这一查询在不同的数据库中实现不相同。在mysql中使用limit关键字,语法:

SELECT 列名 FROM 表名 limit 条数;

比如返回prod_name的前4个:

mysql> SELECT prod_name from Products limit 4; +---------------------+
| prod_name | +---------------------+
| Fish bean bag toy   |
| Bird bean bag toy   |
| Rabbit bean bag toy |
| 8 inch teddy bear | +---------------------+
4 rows in set (0.00 sec)

4. 查询指定位置开始的指定条数

如果想从指定位置查询一定数目的记录,可以使用offset关键字,语法:

SELECT 列名 from 表名 limit 返回结果数目 offset 开始位置;

比如返回从第3条记录开始的4条数据,数据库中的第一条记录位置从0,因此第3条记录的位置为2:

mysql> SELECT prod_name from Products limit 4 offset 2; +---------------------+
| prod_name | +---------------------+
| Rabbit bean bag toy |
| 8 inch teddy bear   |
| 12 inch teddy bear  |
| 18 inch teddy bear | +---------------------+
4 rows in set (0.00 sec)

5.使用where子句过滤数据

SELECT语句中可以根据WHERE子句制定搜索条件进行过滤。例如:

mysql> select prod_id,prod_name,prod_price from Products where prod_price=3.49; +---------+---------------------+------------+
| 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 | +---------+---------------------+------------+
3 rows in set (0.00 sec)

5.1 where操作符

WHERE子句可以使用常用的操作符:

操作符 说明
= 等于
<> 不等于
!= 不等于
> 大于
> = 大于等于
< 小于
<= 小于等于
between A and B 介于A和B之间,包含A和B
IS NULL 为空值

空值检验例子:

mysql> select cust_id,cust_name,cust_email from Customers where cust_email IS NULL; +------------+---------------+------------+
| cust_id | cust_name | cust_email | +------------+---------------+------------+
| 1000000002 | Kids Place    | NULL       |
| 1000000005 | The Toy Store | NULL | +------------+---------------+------------+
2 rows in set (0.00 sec)

5.2AND操作符

mysql> select vend_id,prod_price,prod_name from Products Where vend_id='DLL01' AND prod_price <=4; +---------+------------+---------------------+
| vend_id | prod_price | prod_name | +---------+------------+---------------------+
| DLL01   |       3.49 | Fish bean bag toy   |
| DLL01   |       3.49 | Bird bean bag toy   |
| DLL01 | 3.49 | Rabbit bean bag toy | +---------+------------+---------------------+
3 rows in set (0.00 sec)

5.3OR 操作符

mysql> select vend_id,prod_price,prod_name from Products Where vend_id='DLL01' OR prod_price <=4; +---------+------------+---------------------+
| vend_id | prod_price | prod_name | +---------+------------+---------------------+
| DLL01   |       3.49 | Fish bean bag toy   |
| DLL01   |       3.49 | Bird bean bag toy   |
| DLL01   |       3.49 | Rabbit bean bag toy |
| DLL01 | 4.99 | Raggedy Ann | +---------+------------+---------------------+
4 rows in set (0.00 sec)

5.4IN 操作符

IN操作符用来制定条件范围,范围中的每个条件都可以进行匹配。和OR的功能一样,但比OR有更好的性能。
查询vend_id为”DLL01”或”BRS01”的产品:

mysql> SELECT prod_name,vend_id,prod_price FROM Products WHERE vend_id IN('DLL01','BRS01') ORDER BY prod_price; +---------------------+---------+------------+
| prod_name | vend_id | prod_price | +---------------------+---------+------------+
| Fish bean bag toy   | DLL01   |       3.49 |
| Bird bean bag toy   | DLL01   |       3.49 |
| Rabbit bean bag toy | DLL01   |       3.49 |
| Raggedy Ann         | DLL01   |       4.99 |
| 8 inch teddy bear   | BRS01   |       5.99 |
| 12 inch teddy bear  | BRS01   |       8.99 |
| 18 inch teddy bear | BRS01 | 11.99 | +---------------------+---------+------------+
7 rows in set (0.00 sec)

5.5NOT操作符

WHERE子句操作符的功能是否定其后所跟的任何条件。比如列出出了DLL01之外的所有供应商制造的产品:

 SELECT prod_name,vend_id from Products where NOT vend_id='DLL01' ORDER BY prod_name; +--------------------+---------+
| prod_name | vend_id | +--------------------+---------+
| 12 inch teddy bear | BRS01   |
| 18 inch teddy bear | BRS01   |
| 8 inch teddy bear  | BRS01   |
| King doll          | FNG01   |
| Queen doll | FNG01 | +--------------------+---------+
5 rows in set (0.00 sec)

5.6% 通配符

  1. a%匹配任何以a开头的字符
  2. %b匹配任何以b结尾的字符
  3. %c%c匹配任何包含c的字符

查询以r开头的产品:

mysql> SELECT prod_name,vend_id from Products where prod_name like 'r%' ORDER BY prod_name; +---------------------+---------+
| prod_name | vend_id | +---------------------+---------+
| Rabbit bean bag toy | DLL01   |
| Raggedy Ann | DLL01 | +---------------------+---------+
2 rows in set (0.00 sec)

找出prod_name以F起头以y结尾的产品:


mysql> SELECT prod_name,vend_id from Products where prod_name like 'F%y' ORDER BY prod_name; +-------------------+---------+
| prod_name | vend_id | +-------------------+---------+
| Fish bean bag toy | DLL01 | +-------------------+---------+
1 row in set (0.00 sec)

5.7下划线(_)通配符

一个下划线匹配一个字符:

mysql> select prod_id,prod_name from Products Where prod_name LIKE '__ inch teddy bear'; +---------+--------------------+
| prod_id | prod_name | +---------+--------------------+
| BR02    | 12 inch teddy bear |
| BR03 | 18 inch teddy bear | +---------+--------------------+
2 rows in set (0.00 sec)

5.8通配符使用技巧

  1. 通配符的搜索要消耗更长大处理时间。在其它操作符能达到相同目的的时候应该使用其他操作符
  2. 在确定要使用通配符石尽量不要把通配符永在搜索模式到开始处。
  3. 应该仔细注意通配符的位置.

6.创建计算字段

存储在表中的数据有时候都不是应用程序所需要的,需要从数据库中转换、计算出来,比如有商品的价格和数量,打印报表的时候需要打印出总价,这就是计算字段的概念。

6.1拼接字段

mysql> select CONCAT(cust_name,' (', cust_email,' )') as customer_email From Customers; +------------------------------------------+
| customer_email | +------------------------------------------+
| Village Toys  ([email protected]   ) |
| NULL                                     |
| Fun4All  ([email protected]   )         |
| Fun4All  ([email protected]   )      |
| NULL | +------------------------------------------+
5 rows in set (0.00 sec)

6.2算术计算

Orders中包含所用订单,OrderItems表包含每个订单中的各项物品,检索订单号为20008的所有物品:

mysql> Select prod_id,quantity,item_price FROM OrderItems
 -> WHERE order_num=20008; +---------+----------+------------+
| prod_id | quantity | item_price | +---------+----------+------------+
| RGAN01  |        5 |          5 |
| BR03    |        5 |         12 |
| BNBG01  |       10 |          3 |
| BNBG02  |       10 |          3 |
| BNBG03 | 10 | 3 | +---------+----------+------------+
5 rows in set (0.00 sec)

计算总价:

mysql> SELECT prod_id,quantity,item_price,quantity*item_price as expanded_price FROM OrderItems where order_num=20008; +---------+----------+------------+----------------+
| prod_id | quantity | item_price | expanded_price | +---------+----------+------------+----------------+
| RGAN01  |        5 |          5 |             25 |
| BR03    |        5 |         12 |             60 |
| BNBG01  |       10 |          3 |             30 |
| BNBG02  |       10 |          3 |             30 |
| BNBG03 | 10 | 3 | 30 | +---------+----------+------------+----------------+
5 rows in set (0.00 sec)

7. 使用函数

你可能感兴趣的:(mysql,select)