模糊查詢和正則查詢
like用於在where字句中進行模糊查詢,sql like 字句中使用%字符來表示任意字符。
select * from 表名 where 字段名 like '模糊配的字符%';
mysql中對正則表達式的支持有限, 支持部分正則元字符
select * from 表名 where 字段名 regexp '正則表達式';
排序
order by 字句來設定想按哪個字段哪種方式來進行排序,再返回搜索結果。
select * from 表名 order by 字段;
#默認升序
select * from 表名 order by 字段 desc;
#降序
分頁
limit 字句用於限制由select語句返回的數據數量,或者update,delete語句的操作數量
select * from 表名 where 字段名 limit 數字;
聯合查詢
union 操作符用於連接兩個以上的select語句的結果組合到一個結果集合中,多個select語句刪除重複的數據。
select * from 表名 where 字段條件 union select * from 表名 where 字段條件;
多表查詢
select 字段1,字段2... from 表1,表2... [where 條件]
聚合函數
函數 |
說明 |
avg(字段名) |
返回平均值 |
count(字段名) |
返回行數 |
max(字段名) |
返回最大值 |
min(字段名) |
返回最小值 |
sum(字段名) |
返回和值 |
MySQL普通查詢語句執行順序
3.select …聚合函數 from 表名
1.where …
2.group by …
4.having …
5.order by …
6.limit …;
分組(group by)
select 聚合函數 from 表名 group by 字段名;
having 語句:
#### having語句通常與group by 聯合使用
#### having語句彌補了where關鍵字不能操作聚合函數生成的列‘字段‘
distinct 語句:
#### 不顯示字段的重複值
select distinct 字段名 from 表名;
查詢表記錄時做數學運算
對字段名做運算操作