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;
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';
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;
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>=
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 查詢結果都一樣:
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%';
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;
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;
其他匹配規則:
^ 文本的開始 ;$ 文本的結尾 ;[[:<:>:]] 詞的結尾
總結
以上是生活随笔為你收集整理的mysql 排序 过滤_【MYSQL】-3 排序与过滤的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql int number_Ora
- 下一篇: mysql协议重传,MySQL · 源码