1. 程式人生 > 實用技巧 >MySQL---函式

MySQL---函式

MySQL---函式

數學函式

  • abs(n) 絕對值

select abs(-1), abs(1);
```

  • 近似值 ceil(n) 大於等於引數的最小整數 floor(n) 大於等於引數的最大整數

select ceil(2.4), ceil(-2.4), floor(3.6), floor(-1.2);
```

  • 四捨五入 round(n) round(n,m) 四捨五入保留m位小數

select round(2.5), round(2.5678, 1);
```

  • 保留小數 truncate(n,m) 保留m位小數

select truncate(2.5222, 1);
```

  • 開方 sqrt(n) n的m次方 pow(n,m)

select sqrt(18), pow(2, 3);
```

  • 隨機 rand() (0,1] 的小數

select rand();
```

  • 隨機 1-9 隨機 [a,b] truncate(rand()*(b-a+1)+a,0);
    select truncate(rand() * 9 + 1, 0);
    
  • 求餘數 mod(n,m) n%m

select mod(5, 3);
```

  • 進位制轉換 bin(n) n的二進位制 HEX(n) 獲取16進位制的n
select bin(13), HEX(40);



***

字串函式

  • length(str) 獲取位元組個數

    • char_length(str) 獲取字元個數
    select length('abc12呵呵'), char_length('abc12呵呵');
    
  • substring(str,start,length) 擷取從 start 開始,共計 length 個字元

    • substring(str,start) 擷取從 start 開始,到末尾
    • 注意:字串索引從 1 開始
    select substring('abcdef', 1, 2);
    

select substring('abcdef', 1);
```

  • ASCII(str) 獲取第一個字元在編碼表中對應的整數

    select ASCII('ab');
    
  • concat(str1,str2) 字串拼接

    select concat('abc', 11, true);
    
  • trim(str) 去除兩邊的空格

    select trim('   fds f  ');
    
    • ltrim(str) 去除左邊的空格
    select ltrim(' ---fsdf--   ');
    
    • rtrim(str) 去除右邊的空格
    select rtrim(' ----fafds-   ');
    
  • reverse(str) 翻轉字串

    select reverse('abcdefg');
    
  • lower(str) 轉為小寫

    select lower('AAAAA');
    
  • upper(str) 轉為大寫

    select upper('aaaaa');
    

日期函式

  1. 獲取當前時間
    • current_date() curdate() 獲取當前年月日
    • current_time() curtime() 獲取當前分秒
    • current_timestamp() 獲取當前時間戳
    • now() 獲取當前年月日時分秒
  2. 獲取時間引數
    • year(date) 獲取年
    • month(date) 獲取月
    • dayofmonth(date) 獲取號
    • dayofweek(date) 獲取星期 1-7 對應 日-六
    • hour(date) 小時
    • minute(date) 分
    • second(date) 秒
  3. 時間引數設定指定值
    • date_add(date, interval 值 時間單位); 對 date 操作指定的值
  4. 字串 轉 日期
    • sql select str_to_date('2001-11-23 13:12:11', '%Y-%m-%d %H:%i:%s');
  5. 日期 轉 字串
    • sql select date_format(now(), '%Y-%m-%d %H:%i:%s');
select now(), current_date, current_time, CURRENT_TIMESTAMP;
select curdate();
select curtime();
select now(),
       year(now()),
       month(now()),
       day(now()),
       hour(now()),
       minute(now()),
       second(now()),
       DAYOFWEEK(now());

select date_add(now(), interval 1 second);
# SELECT STR_TO_DATE('00/00/0000', '%m/%d/%Y');

select str_to_date('2001-11-23', '%Y-%m-%d');
select str_to_date('2001/11/23', '%Y/%m/%d');
select str_to_date('2001-11-23 13:12:11', '%Y-%m-%d %H:%i:%s');
# 字串 轉 日期
select date_format(now(), '%Y-%m-%d %H:%i:%s');
# 日期 轉 字串


流程控制

  1. if ( 條件 , true 執行語句 , false 執行語句 )
  2. case 語句
CASE case_value
    WHEN when_value THEN statement_list
    [WHEN when_value THEN statement_list] ...
    [ELSE statement_list]
END CASE

OR:

CASE
    WHEN search_condition THEN statement_list
    [WHEN search_condition THEN statement_list] ...
    [ELSE statement_list]
END CASE
select tage,
       case
           when tage <= 18 then '未成年'
           when tage <= 20 then '成年'
           when tage <= 25 then '大哥'
           else '大叔'
           end
from tab_3;



select tage,
       case tage
           when 18 then '少年'
           when 20 then '成年'
           else '其他'
           end
from tab_3;