MySql字串函數提示

來源:互聯網
上載者:User

標籤:count   提示   dex   小數點   locate   gre   查詢   sub   substr   

1、從左開始截取字串 
left(str, length) 
說明:left(被截取欄位,截取長度) 
例:select left(content,200) as abstract from my_content_t 


2、從右開始截取字串 
right(str, length) 
說明:right(被截取欄位,截取長度) 
例:select right(content,200) as abstract from my_content_t 


3、截取字串 
substring(str, pos) 
substring(str, pos, length) 
說明:substring(被截取欄位,從第幾位開始截取) 
substring(被截取欄位,從第幾位開始截取,截取長度) 
例:select substring(content,5) as abstract from my_content_t 
select substring(content,5,200) as abstract from my_content_t 
(註:如果位元是負數 如-5 則是從後倒數位元,到字串結束或截取的長度) 


4、按關鍵字截取字串 
substring_index(str,delim,count) 
說明:substring_index(被截取欄位,關鍵字,關鍵字出現的次數) 
例:select substring_index("blog.jb51.net","。",2) as abstract from my_content_t 
結果:blog.jb51 
(註:如果關鍵字出現的次數是負數 如-2 則是從後倒數,到字串結束) 

 

5、INSTR(str,substr) ;LOCATE(str,substr); POSITION(str IN substr) 

   返回子串substr在字串str中的第一個出現的位置。這與有2個參數形式的LOCATE()相同,除了參數被顛倒。 

 

6、REVERSE(str)

  反轉str; 

 

7、TRUNCATE(X,D);

返回被捨去至小數點後D位的數字X。若D 的值為 0, 則結果不帶有小數點或不帶有小數部分。可以將D設為負數,若要截去(歸零) X小數點左起第D位開始後面所有低位的值.
今天正好有個需求,查詢標準表中某2個欄位的百分比....

樣本:

select TRUNCATE(progress/video_duration,2) as percent  from mime_study_logs ;
video_duration欄位即使(這裡的分母為0或null),返回的也是null,而不用php計算驗證除法的合法性

這裡計算了兩個數相除,2表示結果小數點後面的位元,比如:1/3 =0.33

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.