SQL学习05——高级数据过滤
Posted xingweikun
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL学习05——高级数据过滤相关的知识,希望对你有一定的参考价值。
组合where子句
and操作符
查询alexa<=50 并且 country='CN’的数据
select name,url,alexa,country from websites where alexa<=50 and country='CN';
+--------+-------------------------+-------+---------+
| name | url | alexa | country |
+--------+-------------------------+-------+---------+
| Taobao | https://www.taobao.com/ | 13 | CN |
| Weibo | http://weibo.com/ | 20 | CN |
+--------+-------------------------+-------+---------+
or操作符
查询alexa大于等于80或者alexa小于20的数据
select name,url,alexa,country from websites where alexa>=80 or alexa<20;
+----------+---------------------------+-------+---------+
| name | url | alexa | country |
+----------+---------------------------+-------+---------+
| Google | https://www.google.cm/ | 1 | USA |
| Taobao | https://www.taobao.com/ | 13 | CN |
| Xuexi | http://www.xxx.cn | 888 | xxx |
| Facebook | https://www.facebook.com/ | 1 | USA |
+----------+---------------------------+-------+---------+
求值顺序
where子句可以包含任意数目的and和or操作符。
接下来的代码练习,我们用一个新的数据表操作
select * 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 dool', '9.49'
select * from products where prod_id='BR01' or prod_id='BR03' and prod_price>=10;
在处理or操作符前,优先处理and操作符。
+---------+--------------------+------------+
| prod_id | prod_name | prod_price |
+---------+--------------------+------------+
| BR01 | 8 inch teddy bear | 5.99 |
| BR03 | 18 inch teddy bear | 11.99 |
+---------+--------------------+------------+
圆括号具有比and或or更高的求值顺序。DBMS首先过滤圆括号内的or条件。
mysql> select * from products where (prod_id='BR01' or prod_id='BR03') and prod_price>=10;
+---------+--------------------+------------+
| prod_id | prod_name | prod_price |
+---------+--------------------+------------+
| BR03 | 18 inch teddy bear | 11.99 |
+---------+--------------------+------------+
in操作符
in操作符用来指定条件范围,范围中的每个条件都可以进行匹配。
mysql> select * from products where prod_id in('BNBG01','BNBG03','RYL02') order by prod_name;
+---------+---------------------+------------+
| prod_id | prod_name | prod_price |
+---------+---------------------+------------+
| BNBG01 | Fish bean bag toy | 3.49 |
| RYL02 | Queen dool | 9.49 |
| BNBG03 | Rabbit bean bag toy | 3.49 |
+---------+---------------------+------------+
in操作符完成了与or相同的工作
not操作符
where子句中的not操作符有且只有一个功能,那就是否定其后所跟的任何条件。
mysql> select * from products where prod_id not in('BNBG01','BNBG03','RYL02') order by prod_name;
+---------+--------------------+------------+
| prod_id | prod_name | prod_price |
+---------+--------------------+------------+
| BR02 | 12 inch teddy bear | 8.99 |
| BR03 | 18 inch teddy bear | 11.99 |
| BR01 | 8 inch teddy bear | 5.99 |
| BNBG02 | Bird bean bag toy | 3.49 |
| RYL01 | King doll | 9.49 |
| RGAN01 | Raggedy Ann | 4.99 |
+---------+--------------------+------------+
mysql> select * from products where not prod_price>=8;
+---------+---------------------+------------+
| 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 |
| RGAN01 | Raggedy Ann | 4.99 |
+---------+---------------------+------------+
以上是关于SQL学习05——高级数据过滤的主要内容,如果未能解决你的问题,请参考以下文章