MySQL之运算符和函数
MySQL中的函數(shù)主要有一下幾類(lèi):
?字符函數(shù);數(shù)值運(yùn)算符和函數(shù);比較運(yùn)算符和函數(shù);日期時(shí)間函數(shù);信息函數(shù);聚合函數(shù);加密函數(shù)
1、字符函數(shù)
1、CONCAT()
root@localhost test>SELECT CONCAT('study','MySQL');可以使用連接符進(jìn)行輸出,如將study和MySQL之間進(jìn)行連接,輸出study-MySQL的形式
root@localhost test>select CONCAT('study','-','MySQL');首先創(chuàng)建一個(gè)存儲(chǔ)名字的數(shù)據(jù)表
root@localhost test>create table name(-> first_name varchar(20),-> last_name varchar(20)-> ); root@localhost test>insert name(first_name,last_name) VALUES ('Tom','.J'),('Jam','.L'),('KoBe','.H');現(xiàn)在為了獲取完整的名字信息,將上述的名字信息輸出,可使用CONCAT命令進(jìn)行輸出
root@localhost test>select CONCAT(first_name,last_name) AS full_name FROM name;2、CONCAT_WS()
而CONCAT_WS指定連接符,第一個(gè)參數(shù)為連接符,第二個(gè)參數(shù)為需要連接的參數(shù)1,第三個(gè)參數(shù)為需要連接的參數(shù)2,以此類(lèi)推
root@localhost test>select CONCAT_WS('|','A','B','C');3、FORMAT()
FORMAT進(jìn)行數(shù)字字符化,之所以把這個(gè)函數(shù)歸結(jié)到字符函數(shù)中,是因?yàn)槠浞祷亟Y(jié)果是一個(gè)字符型
root@localhost test>select FORMAT(12560.75456,2);而如果想保留到整數(shù)位,則將‘2’對(duì)應(yīng)的參數(shù)位置變成0即可
root@localhost test>select FORMAT(12560.75456,0);4、UPPER/LOWER
大小寫(xiě)轉(zhuǎn)換,分別是將大寫(xiě)字母轉(zhuǎn)換成小寫(xiě)字母;把消息字母轉(zhuǎn)換成大寫(xiě)字母
root@localhost test>select UPPER('mysql'); root@localhost test>select LOWER('MYSQL');5、LEFT/RIGHT
從字符串的左側(cè)開(kāi)始獲取;或者從字符串的右側(cè)開(kāi)始獲取,有兩個(gè)參數(shù)
root@localhost test>select LEFT('study-MySQL',5); root@localhost test>select RIGHT('study-MySQL',5);同時(shí)使用LOWER和RIGHT函數(shù)
root@localhost test>select LOWER(RIGHT('study-MySQL',5));當(dāng)然還有幾個(gè)函數(shù)
此外TRIM函數(shù)還有別的用法
root@localhost test>select TRIM(LEADING '?' FROM '??study-MySQL???');表示刪除字符串中的前導(dǎo)的“?”字符,使用關(guān)鍵字LEADING
表示刪除字符串中的后續(xù)的“?”字符,使用關(guān)鍵字TRAILING
刪除前導(dǎo)和后續(xù)所有的?字符,使用關(guān)鍵字BOTH
root@localhost test>select TRIM(BOTH '?' FROM '??study-MySQL???');對(duì)于??study??MySQL??中間的?如何處理呢?通過(guò)使用replace進(jìn)行替換
root@localhost test>select REPLACE('??My??SQL???','?','');在替換的時(shí)候不是說(shuō)一個(gè)字符只能替換一個(gè)字符,可以是一對(duì)多,如:
root@localhost test>select REPLACE('??My??SQL???','?','!!');將一個(gè)?替換成2個(gè)!如上所示。也可以是多對(duì)一,如將兩個(gè)?換成一個(gè)!,如:
root@localhost test>select REPLACE('??My??SQL???','??','!');因?yàn)橛疫呌腥齻€(gè)?,因此只能替換兩個(gè)?,還有一個(gè)?成單了,無(wú)法替換
對(duì)于SUBSTRING是進(jìn)行字符串的截取,有三個(gè)參數(shù),分別是從哪個(gè)字符串中截取,從第幾位開(kāi)始截取,以及截取幾個(gè)參數(shù)
root@localhost test>select SUBSTRING('study-MySQL',7,5);substring(str, pos); substring(str, pos, len)
在字符串study-MySQL中從第7位開(kāi)始截取,截取5個(gè),注意這里與程序中不同,其下標(biāo)是從1開(kāi)始的。
如果只寫(xiě)如兩個(gè)參數(shù),如下表示從這一位開(kāi)始一直截取到字符串的結(jié)尾
root@localhost test>select SUBSTRING('study-MySQL',7); root@localhost test>select SUBSTRING('study-MySQL',-5,5);從字符串的倒數(shù)第5位開(kāi)始,共截取5個(gè)字符
root@localhost test>select SUBSTRING('study-MySQL',-5);從字符串的倒數(shù)第5位開(kāi)始,一直截取到結(jié)束
對(duì)于LIKE和NOT LIKE的匹配與否的問(wèn)題
root@localhost test>select 'MySQL' like 'M%';%這里表示0個(gè)或者多個(gè)字符
這里的“1”表示 true,但這里沒(méi)有體現(xiàn)LIKE的強(qiáng)大功能,現(xiàn)在在數(shù)據(jù)表name中插入一個(gè)記錄
root@localhost test>insert name VALUES ('JJ%','.M');現(xiàn)在想找到名字中包含 J 的記錄,可以如下:
root@localhost test>select * from name WHERE first_name LIKE '%J%';那如果想找到名字中包含%就應(yīng)該
root@localhost test>select * from name WHERE first_name LIKE '%%%';第一個(gè)和最后一個(gè)%表示任意字符,中間的%表示需要查找的字符,但是這樣MySQL認(rèn)為這三個(gè)都是通配符,因此會(huì)找出所有記錄,那如何解決呢?
root@localhost test>select * from name WHERE first_name LIKE '%1%%' ESCAPE 1;告訴系統(tǒng)1后面的這個(gè)字符不需要再進(jìn)行解析,直接認(rèn)為是標(biāo)準(zhǔn)的%字符就可以了,這里不一定非要為1,只要是數(shù)字就可以
另外 “_”匹配的是任意一個(gè)字符,“%”匹配的是任意個(gè)字符。
2、數(shù)值運(yùn)算符和函數(shù)
root@localhost test>select CEIL(3.01); 向上取整,為4 root@localhost test>select FLOOR(3.99); 向下取整,為3 root@localhost test>select 3/4; 普通除法,為0.75 root@localhost test>select 3 DIV 4; 整數(shù)除法,向下取整 root@localhost test>select 5 MOD 3; 取余,等價(jià)于%取余操作 root@localhost test>select 5.2 MOD 3; 取余位2.2 root@localhost test>select 5 % 3; root@localhost test>select POWER(2,3); 冪運(yùn)算,2的3次方 root@localhost test>select ROUND(3.642,2); 保留2位,為3.64 root@localhost test>select ROUND(3.642,1); 保留1位,為3.6 root@localhost test>select ROUND(3.642,0); 只保留到整數(shù),為3.7 root@localhost test>select TRUNCATE(125.89,2);只保留2位,為125.89 root@localhost test>select TRUNCATE(125.89,1);只保留1位,為125.8 root@localhost test>select TRUNCATE(125.89,0);只保留0位,為125 root@localhost test>select TRUNCATE(125.89,-1);為1203、比較運(yùn)算符和函數(shù)
root@localhost test>select 15 BETWEEN 10 AND 20; root@localhost test>select 5 IN(2,3,5,7,9);判斷5在不在序列范圍之間,在的話輸出為1
查看first_name中為空的記錄
4、日期時(shí)間函數(shù)
root@localhost test>select NOW(); 顯示當(dāng)前時(shí)間 root@localhost test>select DATE_ADD('2014-3-13',INTERVAL 365 DAY ); 2015-03-13 root@localhost test>select DATE_ADD('2014-3-13',INTERVAL 1 YEAR ); 2015-03-13 root@localhost test>select DATE_ADD('2014-3-13',INTERVAL -365 DAY ); 2013-03-13 root@localhost test>select DATE_ADD('2014-3-13',INTERVAL 3 WEEK); 2013-03-13 root@localhost test>select DATEDIFF('2014-3-6','2014-4-2'); 日期差值運(yùn)算為-27 root@localhost test>select DATE_FORMAT('2014-4-17','%m/%d/%Y');日期輸出為04/17/20145、信息函數(shù)
而對(duì)于LAST_INSERT_ID,在數(shù)據(jù)表name中沒(méi)有id字段,如果要得到last_insert_id必須在數(shù)據(jù)表中存在一個(gè)自動(dòng)編號(hào)的字段
現(xiàn)在新寫(xiě)入的記錄的ID為6,如果想要通過(guò)函數(shù)得到這個(gè)id可以用:
root@localhost test>select LAST_INSERT_ID();如果同時(shí)寫(xiě)入多條記錄呢,那只會(huì)返回第一條記錄對(duì)應(yīng)的id
root@localhost test>INSERT name(first_name,last_name) VALUES ('KKK','.JJJ'),('AAA','.BBB'); root@localhost test>select USER(); 當(dāng)前登錄用戶 root@localhost test>SELECT VERSION();當(dāng)前版本信息6、聚合函數(shù)
聚合函數(shù)的一個(gè)特點(diǎn)是只有一個(gè)返回值
root@localhost test>select avg(goods_price) AS avg_price FROM goods;計(jì)算數(shù)據(jù)表goods中的平均價(jià)格,保留到小數(shù)點(diǎn)后的兩位
root@localhost test>select ROUND(AVG(goods_price),2) AS avg_price FROM goods; root@localhost test>select COUNT(goods_id) AS coutsID FROM goods; root@localhost test>select MAX(goods_price) AS coutsID FROM goods; root@localhost test>select MIN(goods_price) AS coutsID FROM goods; root@localhost test>select SUM(goods_price) AS coutsID FROM goods;6、加密函數(shù)
可以對(duì)任何一個(gè)信息進(jìn)行MD5的加密,加密后的結(jié)果是一個(gè)32的數(shù)字
root@localhost test>select MD5('admin');而PASSWORD是進(jìn)行密碼的計(jì)算
root@localhost test>select PASSWORD('admin');如果MySQL中是為了以后的Web頁(yè)面做準(zhǔn)備,盡量使用MD5,而PASSWORD是為了修改客戶端密碼
root@localhost test>select PASSWORD = PASSWORD('delimater');將登陸密碼修改為 delimater,退出后再次登陸即可生效。總結(jié)
以上是生活随笔為你收集整理的MySQL之运算符和函数的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: MySQL之无限级分类表设计
- 下一篇: MySQL之alter和upate