mySql常用函數說明

來源:互聯網
上載者:User

標籤:

#mySql的數學函數
select ABS(-5); #絕對值
select ceiling(-5.8); #取大整數
select floor(-5.8); #取小整數
select LEAST(10,3,23,40,0);#取最小數
select GREATEST(1,23,4,6,9,12,9);#取最大數
select MOD(10,3);#除餘
select PI();#π的值
select RAND(2);#隨機數
select ROUND(10.4367,3);#四捨五入 取後面三位小數
select TRUNCATE(10.12321,2); #後兩位截斷函數
select SIGN(3.4);#判斷正負數   負數返回-1 ,正數返回1  0 返回 0
select COS(1);#餘弦函數
select DEGREES(1);#弧度轉換為角度
select Power(2,3);#二的三次方 Pow 簡寫
select SQRT(4);#開方

#彙總函式
select AVG(a.sex) from `user` a; #平均值
select MAX(a.sex) from `user` a; #最大值
select MIN(a.sex) from `user` a; #最小值
select SUM(a.sex) from `user` a; #求和
select COUNT(a.sex) from `user` a; #統計個數
select STD(a.sex)  from `user` a; #標準差

#字串處理函數
select  LENGTH(‘hello‘);#取長度
select  LCASE(‘hello‘);#取小寫
select  UCASE(‘hello‘);#取大寫
select  STRCMP(‘hello‘,‘yes‘);#比較兩個字串的大小(開頭字母的順序)hello>yes:1 hello<yes:-1  等於: 0
select  position(‘yes‘ IN ‘hyesman‘);#字串尋找定位
select  replace(‘yes‘,‘y‘,‘hello‘);#替換字串
select  insert(‘yes man!‘,2,3,‘hello‘);#插入函數2,3位之間插入
select  concat(‘hello‘,‘world‘);#合并函數
select  concat_ws(‘:‘,‘world‘,‘hello‘);#加上間隔符號合并
select  left(‘helloworld‘,3);#取左邊的前三個字元
select  right(‘hello‘,3);#取最後面三個字元
select  ltrim(‘     hello   ‘);#去出左邊空格
select  rtrim(‘     hello   ‘);#去除右邊空格
select  trim(‘     hello   ‘);#去除佐佑兩邊空格
select  substring(‘macket‘,2,3);#從第二位開始往後取三位字串

#日期函數
select  now();#取現在的是時間
select  curtime();#取目前時間
select  curdate();#去現在的時間
select  year(‘20160609‘);#取年份
select  month(‘2016-06-09‘);#取月份
select  monthname(‘2016-06-09‘);#取月份的英文名稱
select  dayofyear(‘2016-06-09‘);#這一年中的第幾天
select  dayofweek(‘2016-06-09‘);#在一個星期中的第幾天
select  dayname(‘2016-06-09‘);#Thursday 取星期
select  hour(‘14:56‘);#    取小時
select  minute(‘14:56‘);#    取分鐘
select  second(‘14:56:23‘);#取秒
select  date_add(now(),interval 3 year);#目前時間往後推三年
select  date_add(now(),interval 3 month);#目前時間往後推三個月
select  date_add(now(),interval 3 day);#目前時間往後推三天
select  date_sub(now(),interval 3 day);#目前時間往前推三天
select  date_sub(now(),interval 3 month);#目前時間往前推三個月
select  date_sub(now(),interval 3 year);#目前時間往前推三年

#對日期格式化函數
#W:星期幾   D:那一天  M:月份  Y:年份  r:目前時間
select date_format(now(),‘%W %D %M %Y %r‘);
select date_format(now(),‘%w %d %m %y %r‘);
#時間,格式化  100個小時 21分,12秒
select time_format(‘100:21:12‘,‘%h:%i %p‘);
#數字轉換IP地址 & IP地址轉換成一個數字
select inet_aton(‘192.168.11.133‘);
select inet_ntoa(‘3232238469‘);#


#類型轉換函式
select 1 + ‘99‘;
select 1 + cast(‘99‘ as signed)

#加了 binary 區分大小寫
select ‘f‘=binary ‘F‘,‘f‘=cast(‘F‘ as binary);#轉二進位
select convert(‘23‘,signed);#數值,轉換資料類型


#mySql的資料加密函數
select password(‘secret‘),password(‘secret‘);#加密過程無法復原,同一字串加密相同
select encrypt(‘secret‘,‘abc‘);#
select encode(‘tanmujiang‘,‘key‘);#互相可逆加密效果,key為解密效果
select decode(encode(‘tanmujiang‘,‘key‘),‘key‘);#解密
select aes_encrypt(‘secret‘,‘key‘);#aes加密
select aes_decrypt(aes_encrypt(‘secret‘,‘key‘),‘key‘);#解密
select MD5(‘secret‘);#MD5加密
select SHA(‘secret‘);#SHA加密


#mySql控制流程函數
select  if(1<10,2,3);#相當於三元運算式
select ifnull(1,2);#如果第一個字元為空白,返回本身 1,否則返回二
select ifnull(null,2);#返回2
select nullif(1,2);#如果第一個參數和第二個參數相等返回1,相等返回null
select nullif(1,1);#返回null

#如果前麵條件成立,返回一個指定值
SELECT
    CASE 4
WHEN 0 THEN
    ‘一等獎‘
WHEN 0 THEN
    ‘二等獎‘
WHEN 3 THEN
    ‘三等獎‘
ELSE
    ‘幸運獎‘
END;

SELECT
    CASE ‘green‘
WHEN ‘red‘ THEN
    100
WHEN ‘green‘ THEN
    200
ELSE
    300
END;


#最後記錄一個逼格高點清空表資料方法
truncate table visit;

mySql常用函數說明

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.