[MySQL光速入門]010 運算符與函數

第五章-思惟導圖.png

必會單詞

  1. regexp 正則表達式
  2. trim 修剪
  3. replace 替換
  4. ceiling 最高限度
  5. year
  6. month
  7. day
  8. hour 小時
  9. minute 分鐘
  10. second
  11. current 當前的
  12. date 日期
  13. time 時間
  14. now 如今
  15. week 星期
  16. version 版本

運算符

算術運算符

  • 加 +正則表達式

    • select int類型的字段,int類型的字段+10 from 表;
    select bookprice,bookprice+10 from book;
    複製代碼
  • 減 -sql

    • select int類型的字段,int類型的字段-10 from 表;
    select bookprice,bookprice-10 from book;
    複製代碼
  • 乘 *函數

    • select int類型的字段,int類型的字段*10 from 表;
    select bookprice,bookprice*10 from book;
    複製代碼
  • 除 /post

    • select int類型的字段,int類型的字段/10 from 表;
    select bookprice,bookprice/10 from book;
    複製代碼
  • 求餘/取模 %ui

    • select int類型的字段,int類型的字段%10 from 表;
    select bookprice,bookprice%10 from book;
    複製代碼

比較運算符

  • 大於/小於spa

    • SELECT 字段1, 字段2 FROM 表 WHERE 字段2 > 值;
    SELECT bookname, bookprice FROM book WHERE bookprice > 20;
    複製代碼
    • SELECT 字段1, 字段2 FROM 表 WHERE 字段2 < 值;
    SELECT bookname, bookprice FROM book WHERE bookprice < 20;
    複製代碼
  • 不等設計

    • SELECT 字段1, 字段2 FROM 表 WHERE 字段2 <> 值;
    SELECT bookname, bookprice FROM book WHERE bookprice <> 20;
    複製代碼
    • SELECT 字段1, 字段2 FROM 表 WHERE 字段2 != 值;
    SELECT bookname, bookprice FROM book WHERE bookprice != 20;
    複製代碼
  • 正則code

    • select 字段1,字段1 REGEXP '正則表達式1',字段2,字段2 REGEXP '正則表達式2',字段3,字段3 REGEXP '正則表達式3' from 表;
    select bookname,bookname REGEXP '^j',bookauthor,bookauthor REGEXP '紅$',bookpublisher,bookpublisher REGEXP '.+出版社' from book;
    複製代碼
    • select * from 表 where 字段 REGEXP '正則表達式';
    select * from book where bookpublisher REGEXP '.+[0-9]$';
    複製代碼

邏輯運算符

  • && andregexp

    • select * from 表名 where 字段 > 值1 and 字段 < 值2;
    select * from book where borrowsum > 5 and borrowsum < 30;
    複製代碼
    • select * from 表名 where 字段 > 值1 && 字段 < 值2;
    select * from book where borrowsum > 5 && borrowsum < 30;
    複製代碼
  • ! notcdn

    • select * from 表名 where not 字段名 = 值;
    select * from book where not borrowsum = 30;
    複製代碼
    • select * from 表名 where 字段名 != 值;
    select * from book where borrowsum != 30;
    複製代碼
    • select * from 表名 where 字段名 <> 值;
    select * from book where borrowsum <> 30;
    複製代碼
  • || or

    • select * from 表名 where 字段 <= 值1 || 字段 >= 值2;
    select * from book where borrowsum <= 5 or borrowsum >= 30;
    複製代碼
    • select * from 表名 where 字段 <= 值1 or 字段 >= 值2;
    select * from book where borrowsum <= 5 || borrowsum >= 30;
    複製代碼
  • 這四個語句, 結果同樣

    select * from book where borrowsum > 5 and borrowsum < 30;
    複製代碼
    select * from book where borrowsum > 5 && borrowsum < 30;
    複製代碼
    select * from book where not( borrowsum <= 5 or borrowsum >= 30);
    複製代碼
    select * from book where not( borrowsum <= 5 || borrowsum >= 30);
    複製代碼

內置函數

字符串函數

  • left(s,n)/right(s,n)

    • select 字段,left(字段,2) from 表;
    select bookname,left(bookname,2) from book;
    複製代碼
    • select 字段,right(字段,2) from 表;
    select bookname,right(bookname,2) from book;
    複製代碼
  • concat()/concat_ws()

    • select 字段1,字段2,字段3,字段4,字段5,CONCAT(字段1,字段2,字段3,字段4,字段5) as 別名1, CONCAT_WS('分隔符',字段1,字段2,字段3,字段4,字段5) as 別名2 from 表;
    select bookid,bookname,bookauthor,bookpublisher,bookprice,CONCAT(bookid,bookname,bookauthor,bookpublisher,bookprice) as 詳情1,CONCAT_WS('_',bookid,bookname,bookauthor,bookpublisher,bookprice) as 詳情2 from book;
    複製代碼
  • trim()/ltrim(s)/rtrim(s)

    • select 字段, trim(字段) 別名1, ltrim(字段) 別名2, rtrim(字段) 別名3 from 表名;
    select bookpublisher,trim(bookpublisher) 刪除左右空格,ltrim(bookpublisher) 刪除左空格,rtrim(bookpublisher) 刪除右空格 from book;
    複製代碼
  • replace()

    • select 字段 別名,replace(字段,'匹配的字段','替換的字段') 替換後 from 表名;
    select bookname 替換前,replace(bookname,'設計','崩潰') 替換後 from book;
    複製代碼
  • substring()

    • select 字段, SUBSTRING(字段,開始位置從一開始,長度) from 表名;
    select bookname, SUBSTRING(bookname,2,3) from book;
    複製代碼

日期函數

  • now()

    select now();
    複製代碼
  • curdate()/curtime()

    select now(),CURRENT_DATE(),CURRENT_TIME(),curdate(),curtime();
    複製代碼
  • dayofweek(d)/dayofmonth(d)/dayofyear(d)

    select now(),DAYOFWEEK(now()),DAYOFMONTH(now()),DAYOFYEAR(now()),WEEKDAY(now());
    複製代碼
  • hour(t)/minute(t)/second(t)

    select now(),HOUR(now()),MINUTE(now()),SECOND(now());
    複製代碼
  • date_add()/date_sub()

    select DATE_ADD(now(),interval 3 day);
    複製代碼
    select DATE_SUB(now(),INTERVAL 7 MINUTE);
    複製代碼
  • datediff()

    select DATEDIFF('2020-10-1',now());
    複製代碼

數學函數

  • abs(x)

    select abs(-789),abs(-123.666);
    複製代碼
  • floor(x)/ceiling(x)

    select FLOOR(-2.3),CEILING(-2.3),FLOOR(9.9),CEILING(9.9);
    複製代碼
  • greatest()/least()

    select GREATEST(1,2,3,4),LEAST(1,2,3,4);
    複製代碼
  • round(x)/truncate(x,y)

    select round(3.4567),round(4.567),TRUNCATE(3.4567,3);
    複製代碼
  • rand()

    select rand(),rand();
    複製代碼
  • sqrt(x)/mod(x,y)

    select sqrt(64),sqrt(2),TRUNCATE(sqrt(2),3),mod(10,4);
    複製代碼

系統函數

  • database()/user()/version()

    select DATABASE(),user(),version();
    複製代碼
  • charset(str)/collation(str)

    select charset('123'),COLLATION('123');
    複製代碼

快速跳轉

相關文章
相關標籤/搜索