1.mysql 關鍵字
關鍵字 | 說明 | 示例 |
---|---|---|
DISTINCT | 指示mysql只返回不同的值 | select distinct vend_id from products; |
limit | 限制結果 | select prod_name from products limit 5; |
DESC | 限定爲降序排序 | select prod_name from products order by prod_name desc; |
ASC | 升序排序,系統默認 | select prod_name from products order by prod_name; |
like | 表示後面語句爲一個pattern | select prod_id from products where prod_name like ‘jet%’; |
regexp | 表示後面爲正則表達式 | select prod_id from products where prod_name regexp ‘.000’ |
binary | 指示區分大小寫 | …where prod_name regexp binary ‘JetPack .000’ |
2.limit,限制結果
mysql> select prod_name from products limit 5;
指示返回不多於5行結果,從第一行開始
mysql> select prod_name from products limit 5,3;
指示返回不多於3行結果,從行5開始
注:mysql中的table從行0開始,即第一行爲行0
和第二個limit語法作用相同的語句:mysql> select prod_name from products limit 3 offset 5;
指示返回不多於3行結果,從行5開始
3.mysql支持的條件運算符
條件運算符 | 說明 |
---|---|
<> | 不等於 |
!= | 不等於 |
BETWEEN | 在指定的兩個值之間 |
NULL | 空值 |
is null | 判斷是否爲空 |
4.mysql操作符:用來連接和改變where子句中的子句的關鍵字,也稱爲邏輯操作符
操作符 | 說明 | 示例 |
---|---|---|
and | 同C語言&& | select prod_name from products where vend_id = 1003 and prod_price >=10; |
or | 同C語言|| | select prod_name from products where vend_id = 1003 or vend_id = 1002; |
in | 和or的作用相同 | select prod_name from products where vend_id in(1002,1003); |
not | 同C語言!,取反 | select prod_name from products where vend_id not in(1002,1003); |
5.通配符,like打頭表示爲通配pattern
wildcard | 說明 | 示例 |
---|---|---|
% | 匹配任意字符出現任意次數 | select prod_id,prod_name from products where prod_name like ‘jet%’; |
_ | 只能匹配單個字符 | select prod_id,prod_name from products where prod_name like ‘_ ton anvil’; |
注意:通配符%可以匹配任何字符,但不能匹配NULL
6.正則表達式,由關鍵字regexp起頭表示
特殊字符 | 說明 | 示例 |
---|---|---|
. | 匹配任意一個字符 | …where prod_name regexp ‘.000’; |
| | 正則表達式中的或操作 | …where prod_name regexp ‘1000|2000’; |
\\ | mysql的轉義字符 | …where vend_name regexp ‘\.’; |
^,$ | 位置錨定 | —— |
7.創建計算字段
concat函數:拼接字段;例:
select concat(rtrim(vend_name),' (',rtrim(vend_country),')') as vendor_title from vendors;
8.數據處理函數
[1]文本處理函數
函數 | 說明 | 函數 | 說明 |
---|---|---|---|
left() | 返回串左邊的字符 | right() | 返回串右邊的字符 |
length() | 返回串的長度 | locate() | 找出串的一個子串 |
lower() | 將串轉換爲小寫 | upper() | 將串轉換爲大寫 |
Ltrim() | 去丟串左邊的空格 | Rtrim() | 去丟串右邊的空格 |
soundex() | 返回串的soundex值 | substring() | 返回子串的字符 |
[2]日期和時間處理函數
函數 | 說明 | 函數 | 說明 |
---|---|---|---|
date() | 返回日期時間的日期部分 | time() | 返回日期時間的時間部分 |
hour() | 返回時間小時部分 | minute(),second() | 返回時間分鐘,秒部分 |
year() | 返回日期的年份部分 | month(),day() | 返回日期月份,天數部分 |
now() | 返回當前日期和時間 | dayofweek() | 給定日期返回星期幾 |
[3]數值處理函數
函數 | 說明 | 函數 | 說明 | 函數 | 說明 |
---|---|---|---|---|---|
abs() | 返回絕對值 | rand() | 返回一個隨機數 | tan() | 返回角度的正切 |
exp() | 返回一個數的指數 | pi() | 返回圓周率 | sqrt() | 返回平方根 |
cos() | 返回一個角度的餘弦 | sin() | 返回一個角度的正弦 | mod() | 返回除操作的餘數 |
9.彙總數據函數
函數 | 說明 | 示例 |
---|---|---|
avg(列名) | 返回列的均值 | select avg(prod_price) as avg_price from products; |
count(*) | 返回這個表的行數 | select count(*) as num_prod from products; |
count(列名) | 得該列行數,不含NULL | select count(vend_id) as num_id from products; |
max(列名) | 返回某列最大值 | select max(vend_id) as max_id from products; |
min(列名) | 返回某列最小值 | select min(vend_id) as min_id from products; |
sum(列名) | 返回某列值之和 | select sum(vend_id) as sum_id from products; |
注意:在對列操作的聚集函數中可以使用distinct關鍵字,限定操作對象
10.數據分組
group by創建分組,然後對每個組的結果進行聚集。
select vend_id,count(*) as num_prods from products group by vend_id;
having過濾分組,示例如下:
select vend_id,count(*) as num_prods from products group by vend_id having count(*)>=3;
select 子句順序
SELECT–>FROM–>WHERE–>GROUP BY–>HAVING–>ORDER BY–>LIMIT