SQL第4-6课数据过滤(WHERE子句)

数据库准备:

mysql> show databases
    -> ;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| firstSQL           |
| mysql              |
| test               |
+--------------------+
4 rows in set (0.01 sec)

mysql> use firstSQL;
Database changed
mysql> show tables;
+--------------------+
| Tables_in_firstSQL |
+--------------------+
| Customers          |
| OrderItems         |
| Orders             |
| Products           |
| Vendors            |
+--------------------+
5 rows in set (0.00 sec)

mysql> SELECT prod_name,prod_price
    -> FROM Products
    -> WHERE prod_price = 3.49;
+---------------------+------------+
| prod_name           | prod_price |
+---------------------+------------+
| Fish bean bag toy   |       3.49 |
| Bird bean bag toy   |       3.49 |
| Rabbit bean bag toy |       3.49 |
+---------------------+------------+
3 rows in set (0.00 sec)

ORDER BY 应位于WHERE 之后。

操作符 描述
= 等于
<> 不等于
> 大于
< 小于
>= 大于等于
<= 小于等于
BETWEEN 在某个范围内
LIKE 搜索某种模式
mysql> SELECT prod_id ,prod_price,prod_name
    -> FROM Products
    -> WHERE vend_id = 'DLL01' AND prod_price <= 4;
+---------+------------+---------------------+
| prod_id | prod_price | prod_name           |
+---------+------------+---------------------+
| BNBG01  |       3.49 | Fish bean bag toy   |
| BNBG02  |       3.49 | Bird bean bag toy   |
| BNBG03  |       3.49 | Rabbit bean bag toy |
+---------+------------+---------------------+
3 rows in set (0.00 sec)
mysql> SELECT prod_name,prod_price
    -> FROM Products
    -> WHERE vend_id IN ('DLL01','BRS01')
    -> ORDER BY prod_name;
+---------------------+------------+
| prod_name           | prod_price |
+---------------------+------------+
| 12 inch teddy bear  |       8.99 |
| 18 inch teddy bear  |      11.99 |
| 8 inch teddy bear   |       5.99 |
| Bird bean bag toy   |       3.49 |
| Fish bean bag toy   |       3.49 |
| Rabbit bean bag toy |       3.49 |
| Raggedy Ann         |       4.99 |
+---------------------+------------+
7 rows in set (0.00 sec)
mysql> SELECT prod_name,prod_price
    -> FROM Products
    -> WHERE  NOT vend_id IN ('DLL01','BRS01')
    -> ORDER BY prod_name;
+------------+------------+
| prod_name  | prod_price |
+------------+------------+
| King doll  |       9.49 |
| Queen doll |       9.49 |
+------------+------------+
通配符过滤
mysql>  SELECT prod_name,prod_id
    -> FROM Products
    ->  WHERE prod_name LIKE 'Fish%';
+-------------------+---------+
| prod_name         | prod_id |
+-------------------+---------+
| Fish bean bag toy | BNBG01  |
+-------------------+---------+
1 row in set (0.00 sec)

大小写是敏感的,% 不匹配NULL。

mysql>  SELECT prod_name,prod_id
    -> FROM Products
    ->  WHERE prod_name LIKE '__ inch teddy bear';
+--------------------+---------+
| prod_name          | prod_id |
+--------------------+---------+
| 12 inch teddy bear | BR02    |
| 18 inch teddy bear | BR03    |
+--------------------+---------+
2 rows in set (0.00 sec)
mysql> SELECT cust_contact
    -> FROM Customers
    -> WHERE NOT cust_contact LIKE '[JM]%'
    -> ORDER BY cust_contact;
+--------------------+
| cust_contact       |
+--------------------+
| Denise L. Stephens |
| Jim Jones          |
| John Smith         |
| Kim Howard         |
| Michelle Green     |
+--------------------+
5 rows in set (0.00 sec)

你可能感兴趣的:(SQL第4-6课数据过滤(WHERE子句))