MySQL基础查询子句 where /order by/通配符过滤 like/正则表达式过滤 regexp

##第4章 检索数据
#检索单个列、多个列、所有列
select prod_name from products;
select prod_id,prod_name,prod_price from products;
select * from products;

#检索不同的行(唯一行)
select vend_id from products;
select distinct vend_id from products;
select distinct vend_id,prod_price from products;

#限制结果(针对行)
select prod_name from products limit 5;
select prod_name from products limit 5,5;#取6\7\8\9\10行
select prod_name from products limit 10,5;#取11\12\13\14行

#完全限定的表名
select products.prod_name from products;
select products.prod_name from crashcourse.products;

 


##第5章 排序检索出的数据(order by)
#排序数据(单列)
#注:如果不明确规定排序顺序,则不应该假定检索出的数据的顺序有意义
select prod_name from products order by prod_name;

#多列排序
select prod_id,prod_price,prod_name from products order by prod_price,prod_name;

#指定排序方向
#desc:降序,从大到小;asc:升序,从小到大,默认状态
select prod_id,prod_price,prod_name from products order by prod_price desc;
select prod_id,prod_price,prod_name from products order by prod_price desc,prod_name;

select prod_price from products order by prod_price desc limit 1;

 


##第6、7、8、9章  过滤数据(where)
#查找单个值(< > <= >=)
select prod_name,prod_price from products where prod_price=2.5;
select prod_name,prod_price from products where prod_name="fuses";
select prod_name,prod_price from products where prod_price < 10;
select prod_name,prod_price from products where prod_price <= 10;

#不匹配查找(<>  !=)
select vend_id,prod_name from products where vend_id <> 1003;
select vend_id,prod_name from products where vend_id != 1003;

#范围值查找(between a and b)
select prod_name,prod_price from products where prod_price between 5 and 10;

#空值查找(is null)
select prod_name from products where prod_price is null;
select cust_id from customers where cust_email is null;

#and 操作符
select prod_id,prod_name,prod_price from products where vend_id =1003 and prod_price<=10;

#or 操作符
select prod_name,prod_price from products where vend_id=1002 or vend_id=1003;

/*
and 与 or 连用 ,注意顺序
and在计算次序中优先于or,圆括号优先于and
例如:列出价格为10美元及以上且由1002或1003制造的所有产品的名称和价格
*/
#错误示范(理解为供货商为1003且价格大于等于10,或者由供货商1002提供的)
select prod_name,prod_price from products where vend_id=1002 or vend_id=1003 and prod_price>=10; 
#正确做法
select prod_name,prod_price from products where (vend_id=1002 or vend_id=1003) and prod_price>=10;

#in 操作符
select prod_name,prod_price from products where vend_id in (1002,1003) order by prod_name;
#与上式等价
select prod_name,prod_price from products where vend_id=1002 or vend_id=1003  order by prod_name;
/*
in 操作符的优点:
1、更清楚直观
2、计算次序更容易管理
3、比or操作符执行更快
4、可以包含其它select语句
*/

#not 操作符
select prod_name,prod_price from products where vend_id not in (1002,1003) order by prod_name;
/*
MySQL支持使用not对in、between、exists子句取反,这与多数其他DBMS允许使用not对各种条件取反有很大差别
*/

##通配符过滤
#like 操作符
#%通配符 (匹配多个字符)
select prod_id,prod_name from products where prod_name like "jet%"; #找出所有以jet开头的产品
select prod_id,prod_name from products where prod_name like "%anvil%"; #找出所有包含文本anvil的产品
select prod_id,prod_name from products where prod_name like "s%e";#找出所有以s开头以e结尾的产品

#_通配符(匹配单个字符)
select prod_id,prod_name from products where prod_name like "_ ton anvil";
select prod_id,prod_name from products where prod_name like "% ton anvil";

/*
技巧:
1、不要过度使用通配符,若其他操作符能达到相同目的,优先使用其他操作符
2、尽量不要把通配符用在搜索模式的开始处,因为搜索速度慢
*/


##正则表达式
#基本字符匹配
#检索prod_name中包含文本"1000"的行
select prod_name from products where prod_name regexp '1000' order by prod_name;
select prod_name from products where prod_name like '%1000%' order by prod_name;#与上式等价
#.表示匹配任意一个字符
select prod_name from products where prod_name regexp '.000' order by prod_name; 

#or匹配(a|b|c 、 [abc] 都表示:a或b或c)
select prod_name from products where prod_name regexp '1000|2000' order by prod_name;
select prod_name from products where prod_name regexp '1000|2000|3000' order by prod_name;
select prod_name from products where prod_name regexp '[1|2|3]000' order by prod_name;

select prod_name from products where prod_name regexp '[123] Ton' order by prod_name; #[123] Ton=[1|2|3] Ton 
select prod_name from products where prod_name regexp '1|2|3 Ton' order by prod_name #1|2|3 Ton=1或2或3 Ton
select prod_name from products where prod_name regexp '[^123]' order by prod_name; #^表示否定,指匹配除指定字符外的任何东西

#匹配范围
#[0123456789]=[0-9]
select prod_name from products where prod_name regexp '[1-5] Ton' order by prod_name;#[1-5] Ton=1 Ton或2 Ton或3 Ton或4 Ton或5 Ton

#匹配特殊字符(特殊字符例如:. [] | -)
#查找prod_name中含有.的行
select prod_name from products where prod_name regexp '\\.' order by prod_name; #\\为转义字符
select vend_name from vendors where vend_name regexp '\\.' order by vend_name;

/*
匹配字符类:
    类                                     说明
[:alnum:]        任意字母和数字       =[a-aA-Z0-9]
[:alpha:]         任意字符                  =[a-zA-Z]
[:blank:]         空格和制表               =[\\t]
[:cntrl:]           ASCII控制字符         =ASCII0到31和127
[:digit:]           任意数字                   =[0-9]
[:graph:]        与[:print:]相同,但不包含空格
[:lower:]        任意小写字母            =[a-z]
[:print:]          任意可打印字符
[:punct:]        即不在[:alnum:]又不在[:cntrl:]中的任意字符
[:space:]       包含空格在内的任意空白字符 =[\\f\\n\\r\\t\\v]
[:upper:]       任意大写字母             =[A-Z]
[:xdigit:]        任意十六进制数字      =[a-fA-F0-9]
*/

#匹配多个实例
/*
元字符              说明
*                  0个或多个匹配 
+                 1个或多个匹配={1,}
?                0个或1个匹配={0,1}
{n}               指定数目的匹配
{n,}             不少于指定数目的匹配
{n,m}           匹配数目的范围(m不超过255)
*/
select prod_name from products where prod_name regexp '\\([0-9] sticks?\\)' order by prod_name;
select prod_name from products where prod_name regexp '\\([[:digit:]] sticks?\\)' order by prod_name;#与上式等价,s?表示s可选,出现0次或1次
#匹配连在一起的任意4位数字
select prod_name from products where prod_name regexp '[0-9]{4}' order by prod_name;
select prod_name from products where prod_name regexp '[[:digit:]]{4}' order by prod_name;
select prod_name from products where prod_name regexp '[0-9][0-9][0-9][0-9]' order by prod_name;

#定位符
/*
元字符           说明
^               文本的开始 
$               文本的结尾
[[:<:]]         词的开始
[[:>:]]         词的结尾
*/
#找出以一个数(包括以小数点开始的数)开始的所有产品

select prod_name from products where prod_name regexp '^[0-9\\.]' order by prod_name;
select prod_name from products where prod_name regexp '^[[:digit:]\\.]' order by prod_name;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值