MySQL 相關函式

2021-10-03 17:24:05 字數 3162 閱讀 5893

%   0或者多個字元

_    任意乙個字元

[charlist]     字元列中的任何單一字元

[^charlist]    or [!charlist]     不在字元列中的任何單一字元

^[charlist]     以字元列中任何單一字元開頭的

下面的 sql 語句選取 name 以 "g"、"f" 或 "s" 開始的所有**:

select * from websites

where name regexp '^[gfs]';

^[^charlist]   不以字元列中任何單一字元開頭的

mysql裡使用正規表示式     regexp

concat ('1','2','3')  --------->123

concat可以連線乙個或者多個字串,但是如果字串中有乙個為null,則結果返回null

concat_ws('separator','a','b','c')

concat_ws('x','a','b','c')---------->     axbxc

concat_ws('!','a','b','c')-----------> a!b!c

如果被拼接的字串裡面有乙個null

concat_ws('!','a','b',null)------->a!b

group_concat(distinct 需要連線字段   order by  col_name asc/desc    separator  '分隔符')  from  表名   group by   ***x

idname1a

1a1b

2c2d

3eselect  id,group_concat(name    order by id asc   separator '!')  from  tb group by id 

1  a!a!b 

2  c!d

3   e

兩大功能

1 將表table的字段column1的abc替換成·def

update table set column1=replace(column1,'abc','def');

2將str字串中from_str都換成to_str

replace(str,from_str,to_str)

例子"monaco-ville"是合併國家名字 "monaco" 和延伸詞"-ville".

顯示國家名字,及其延伸詞,如首都是國家名字的延伸。

你可以使用sql函式 replace 或 mid.

select name,replace(capital,name,'')

from world

where capital like concat(name,'%')

andcapital!=name;

sql server 中 text或ntext 字段內容替換

剛開始,update aa 表 set xx欄位=replace(xx欄位,"要替換的","特定串") ,出現錯誤:函式 replace 的引數 1 的資料型別 ntext 無效。update article set heading=replace(convert(nvarchar(4000),heading),'','')

1update表名

2settext型別欄位名=replace(convert(varchar(8000),text型別欄位名),'要替換的字元','替換成的值')

varchar和nvarchar型別是支援replace,所以如果你的text/ntext不超過8000/4000可以先轉換成前面兩種型別再使用replace。

1update表名

2settext型別欄位名=replace(convert(varchar(8000),text型別欄位名),'要替換的字元','替換成的值')

1update表名

2setntext型別欄位名=replace(convert(nvarchar(4000),ntext型別欄位名),'要替換的字元','替換成的值')

在mysql中,round函式用於資料的四捨五入,它有兩種形式:

1、round(x,d)  ,x指要處理的數,d是指保留幾位小數

這裡有個值得注意的地方是,d可以是負數,這時是指定小數點左邊的d位整數字為0,同時小數字均為0;

2、round(x)  ,其實就是round(x,0),也就是預設d為0;

下面是幾個例項

1、查詢: select round(1123.26723,2);

結果:1123.27

2、查詢: select round(1123.26723,1);

結果: 1123.3

3、查詢: select round(1123.26723,0);

結果:1123

4、查詢: select round(1123.26723,-1);

結果: 1120

5、查詢: select round(1123.26723,-2);

結果:1100

5、查詢: select round(1123.26723);

結果:1123

mysql相關函式 MySql 相關函式

select group concat column name from table name group by table name,table name2.field 函式自定義排序 select from user where type in 1,2,3 order by field colu...

MySQL日期相關函式

1.獲取當前日期 curdate current date current date mysql select curdate mysql select curtime 另外獲取utc 全球吧標準時間,原先也被稱作格林威治標準時間或gmt 時間,本地時間 utc 時間 8 小時,相應的函式是 utc...

mysql中的length函式相關

引用 在mysql中length是計算欄位的長度乙個漢字是算三個字元,乙個數字或字母算乙個字元了,與char length是有一點區別,本文章重點介紹第乙個函式。mysql裡面的length函式是乙個用來獲取字串長度的內建函式。具體用法示例如下 1 檢視某字串的長度 比如本站 select leng...