mysql 排序 过滤_【MYSQL】-3 排序与过滤

上周加入数据蛙二期培训,结束了孤独战斗的现状。断断续续自学了3个月(当然看了各种视频和各种书,一把辛酸泪。。。),现在选择报班,主要还是觉得一个靠谱的组织和团队,可以极大缓解我学习过程中不时闪现的焦虑和无助,最重要的是少走弯路。毕竟青春不再,年华易逝,浪费可耻哈哈哈哈~

本文结合个人情况,依据学习规划,对《mysql必知必会》前9章内容关于排序和过滤操作进行知识点整理。

一 . 排序

子句排序:

select [column1] from [table] order by [column2],[column3]…..

column1 与 column2 不要求相等,也可以多列排序。

select prod_name from products order by prod_name;

升降顺序:默认升序 asc;降序 desc

select prod_id,prod_price,prod_name from products

order by prod_price desc,prod_name;

与 limit 组合找到最值

select prod_price from products

order by prod_price desc

limit 1;

a6178b0e5653?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

二 . 过滤

1. 按照指定条件搜索数据

where:select [column1],[column2] from [table] where [column3]=x

特性1:匹配时不区分大小写;

特性2: 用单引号限定字符串;

select prod_name,prod_price from products

where prod_name='fuses';

a6178b0e5653?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

特性3:匹配范围可以用 between ...and...;in ——见特性7

特性4:where 和 order by 同时使用时,order by 放后面;

select prod_name,prod_price from products

where prod_price between 5 and 10

order by prod_price;

a6178b0e5653?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

特性5:用于检查null值

select cust_id from customers

where cust_email is null;

特性6:结合逻辑操作符and & or,and 具有更高优先级

select vend_id,prod_name,prod_price

from products

where vend_id=1002 or vend_id=1003 and prod_price>=

a6178b0e5653?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

这里搜索出来的是1003厂商制造的价格小于10美元的数据,和1002制造的数据。

特性7:匹配范围还可以用 in(小值,大值)

select prod_name,prod_price from products

where vend_id in(1002,1003)

order by prod_name;

同义转换:功能与 or 相当

select prod_name,prod_price from products

where vend_id =1002 or vend_id =1003

order by prod_name;

这里用in 和or 查询结果都一样:

a6178b0e5653?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

in操作符的优势

1) 在使用长的合法选项清单时,IN操作符的语法更清楚且更直观。

2) 在使用IN时,计算的次序更容易管理(因为使用的操作符更少)。

3) IN操作符一般比OR操作符清单执行更快。

4) IN的最大优点是可以包含其他SELECT语句,使得能够更动态地建立WHERE子句

not 取反,找出与条件列不匹配的行

select prod_name,prod_price

from products

where vend_id not in (1002,1003)

order by prod_name;

2. like:运用通配符进行过滤

%:任何字符出现任意次数 (....where [column] like 'a%'):列 column 中以a开头的所有行;

select prod_id,prod_name

from products

where prod_name like 'jet%';

a6178b0e5653?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

注意:% 可以匹配尾空格;不能匹配null值

_:匹配任意单个字符

select prod_id,prod_name

from products

where prod_name like '_ ton anvil';

通配符使用注意事项:

1)优先选择其他操作符;

2)使用时最好不要用在搜索模式的开始位置,会使得搜索变慢。

3)注意通配符放置的位置

3. 复杂过滤:正则表达式

regexp:...where [column] regexp 'abc'

完全匹配:

select prod_name from products

where prod_name regexp'%1000'

order by prod_name;

部分匹配:. :任意一个字符

select prod_name from products

where prod_name regexp '.000'

order by prod_name;

a6178b0e5653?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

注意,这里换成like,是不输出结果的。

select prod_name from products

where prod_name regexp'1000'

order by prod_name;

为什么呢?

regexp vs like:

1)like 匹配整个列,regex在列值内匹配。比如匹配1000,like会在一列中查找字符为1000的值,但是regexp在每一个值中找到含有字符1000的值;

2)like 必须和通配符结合使用,否则不返回结果。

或匹配(or):a|b—— 匹配 a 或者 b

匹配多项中的一项:[123]-匹配1或2或3

匹配特殊字符:前面加\

匹配范围:[1-9]

select prod_name

from products

where prod_name regexp '[1-8] ton'

order by prod_name;

其他匹配规则:

^ 文本的开始 ;$ 文本的结尾 ;[[:<:>:]] 词的结尾

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值