MySQL(六)運算子與函式

2021-08-29 04:14:05 字數 3069 閱讀 9249

1.運算子

+加法、-減法、*乘法、/除法、%取餘、

比較運算子

檢視年齡在19到21之間的

檢視以姓張開頭名字是兩個字的資訊

檢視**以135開頭的資訊

邏輯運算子

and與、or或、not非

查詢位址在北京、並且年齡大於18的資訊

2.數值函式

2.1

ceil(x):獲取返回大於x的最小整數值。

列:select ceil(28.5);

結果:29

2.2

floor(x):返回小於x的最大整數值。

列:select floor(28.5);

結果:28

2.3

round(x):返回最接近於x的整數,對x進行四捨五入。

列:select round(28.55);

結果:29

round(x,y):返回接近於x的引數,其值保留到小數點後的y位,若y位負值,則保留到小數點左邊y位。

列:select round(28.55,1),round(28.55,0),round(28.55,-1);

結果:28.6 、29、30

2.4截斷函式

truncate(x,y):返回被捨去至小數點後y位的數字x。若y值為0,則結果為整數、若y的值為負數,則截去x小數點左起第y位開始後面所有地位的值。

列:select truncate(28.55,1),truncate(28.55,0),truncate(28.55,-1);

結果:28.5、28、20

2.5

mod(x.y):返回x被y除后的餘數

列:select mod(11,2);

結果:1

3.字元函式

3.1

concat(s1,s2…):返回結果為連線引數產生的字串,如果任何乙個引數為null,則返回結果null;

列:select concat(『hello』,『world』);

結果:helloworld

concat_ws(x,s1,s2…):第乙個引數x是其他引數的分隔符,如果分隔符為null,則結果為null。

列:select concat_ws(』-』,『hello』,『world』);

結果:hello-world

3.2字母轉換大小寫

lower(str):將str中的字母字元全部轉換成小寫字母。

列:select lower(『hello world』);

結果:hello world

upper(str):將str中的字母字元全部轉換成大寫字母。

列:select upper(『hello world』);

結果:hello world

3.3求字串長度

length(str):返回值為字串的位元組長度。

列:select length(『hello』);

結果:5

3.4刪除空格

ltrim(str):刪除字元左側空格、返回字串str。

rtrim(str):刪除字元右側空格、返回字串str。

trim(str):刪除字元兩側空格、返回字串str。

3.6擷取字串

substring(str,n,len):str需要擷取的字串,n起始位置,len擷取的長度。n如果是負數,則子字串位置起始於字串結尾的n個字元。

列:select substring(『hello world』,1,5);

結果:hello

列:select substring(『hello word』,-3,2);

結果:rl

3.7獲取指定長度字串

left(s,n):返回字串s開始的最左邊n個字元。

列:select left(『hello world』,5);

結果:hello

right(s,n):返回字串中最右邊n個字元。

列:select right(『hello world』,5);

結果:world

3.8替換函式

replace(str,from_str,to_str):str原字串from_str要被替換的字串to_str被替換的字串。

列:select replace(『hello world』,『world』,『mysql』);

結果:hello mysql

3.9格式化函式

format(x,n):將數字x格式化,並以四捨五入的方式保留小數點後n位,結果以字串的形式返回。若n為0,則返回結果不含小數部分。

列:select format(1234.5678,2),format(1234.5,2),format(1234.5678,0);

結果:1234.57,1234.50,1235

NOT運算子與 運算子

6.4.2 not運算子與 運算子 對於簡單的條件查詢,not運算子與 運算子的功能幾乎沒有什麼區別,那麼not運算子的優勢體現在 呢?答案是它可以與其他運算子組合使用,這一點是 運算子所不能實現的。在6.4.1節已經介紹了not運算子與in運算子組合使用的例子,下面給出乙個not運算子與betwe...

MySQL 運算子和函式

sql萬用字元 萬用字元描述 替代乙個或多個字元 僅替代乙個字元 charlist 字元列中的任何單一字元 charlist 或者 charlist 不在字元列中的任何單一字元 例如 select from customers where city like bsp 等價於 concat ws 第乙...

mysql運算子,比較運算子

我也是菜鳥,也是新手,一起學習,一起進步,加油 首先 比較運算子,進行比較之後的 結果如果為真 返回1,結果為假 返回 0 一下為常用的,最基礎的一些沒有列出來 或 不等於 null 的安全等於 null safe between 存在於指定範圍 in存在於指定集合 is null 為 null i...