mysql截取字串與reverse函數

來源:互聯網
上載者:User

標籤:等價   special   找不到   string   字串翻轉   注意   last   利用   題目   

mysql的函數大全:

http://www.jb51.net/Special/606.htm

這個網頁上很多知識點,可以學習下,關於mysql的函數,也可以作為API查詢:

這裡只說下mysql的截取函數和reverse函數:

MySQL 字串截取函數:left(), right(), substring(), substring_index()。還有 mid(), substr()。其中,mid(), substr() 等價於 substring() 函數,substring() 的功能非常強大和靈活。

1. 字串截取:left(str, length)

mysql> select left(‘example.com‘, 3);
+-------------------------+
| left(‘example.com‘, 3) |
+-------------------------+
| exa                     |
+-------------------------+
2. 字串截取:right(str, length)

mysql> select right(‘example.com‘, 3);
+--------------------------+
| right(‘example.com‘, 3) |
+--------------------------+
| com                      |
+--------------------------+

執行個體:

#查詢某個欄位後兩位字元
select right(last3, 2) as last2 from historydata limit 10;
#從應該欄位取後兩位字元更新到另外一個欄位
update `historydata` set `last2`=right(last3, 2);


3. 字串截取:substring(str, pos); substring(str, pos, len)

3.1 從字串的第 4 個字元位置開始取,直到結束。

mysql> select substring(‘example.com‘, 4);
+------------------------------+
| substring(‘example.com‘, 4) |
+------------------------------+
| mple.com                   |
+------------------------------+
3.2 從字串的第 4 個字元位置開始取,只取 2 個字元。

mysql> select substring(‘example.com‘, 4, 2);
+---------------------------------+
| substring(‘example.com‘, 4, 2) |
+---------------------------------+
| mp                              |
+---------------------------------+
3.3 從字串的第 4 個字元位置(倒數)開始取,直到結束。

mysql> select substring(‘example.com‘, -4);
+-------------------------------+
| substring(‘example.com‘, -4) |
+-------------------------------+
| .com                          |
+-------------------------------+
3.4 從字串的第 4 個字元位置(倒數)開始取,只取 2 個字元。

mysql> select substring(‘example.com‘, -4, 2);
+----------------------------------+
| substring(‘example.com‘, -4, 2) |
+----------------------------------+
| .c                               |
+----------------------------------+
我們注意到在函數 substring(str,pos, len)中, pos 可以是負值,但 len 不能取負值。

4. 字串截取:substring_index(str,delim,count)

4.1 截取第二個 ‘.‘ 之前的所有字元。

mysql> select substring_index(‘www.example.com‘, ‘.‘, 2);
+------------------------------------------------+
| substring_index(‘www.example.com‘, ‘.‘, 2) |
+------------------------------------------------+
| www.example                               |
+------------------------------------------------+
4.2 截取第二個 ‘.‘ (倒數)之後的所有字元。

mysql> select substring_index(‘www.example.com‘, ‘.‘, -2);
+-------------------------------------------------+
| substring_index(‘www.example.com‘, ‘.‘, -2) |
+-------------------------------------------------+
| example.com                                          |
+-------------------------------------------------+
4.3 如果在字串中找不到 delim 參數指定的值,就返回整個字串

mysql> select substring_index(‘www.example.com‘, ‘.coc‘, 1);
+---------------------------------------------------+
| substring_index(‘www.example.com‘, ‘.coc‘, 1) |
+---------------------------------------------------+
| www.example.com                               |
+---------------------------------------------------+

 

mysql的reverse函數:

題目:

/a/b/c/d/123.jpg

e/w/r/t/345.jpg

/2/3/5/6/567.jpg

請用mysql來擷取最後的圖片名稱:

方案:先利用reverse函數,將這些字串翻轉

         然後利用mysql的index來截取字串

         截取了之後再翻轉過來即可

因為mysql不像java一樣可以截取最後斜杠後面的內容,所以必須採取這種方式

 

mysql截取字串與reverse函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.