注意:本文出自 “阿飛”的部落格 ,如果要轉載本文章,請與作者聯絡!並註明來源: http://blog.csdn.net/faye0412/article/details/7915300
這兩天一直在協助同事做資料匯入模組,也遇到了不少問題,比如前一篇文章裡提到的CSV大檔案匯入MYSQL,匯入MYSQL後,關鍵字什麼的資料整理和複製等等,還有sql中涉及到了一些字串的截取等處理....
本文就MYSQL中對字串截取操作做個小結,將字串函數使用做個使用說明並附各種例子,具體如下:
————————————————————————————————————————
在這裡只對常用的MySQL字串截取函數進行舉例說明:left(), right(), substring()和 substring_index()。 還有 mid(), substr()。
其中,mid(),substr() 等價於 substring() 函數,substring() 的功能非常強大和靈活。各個例子如下所示(建表SQL略):
1. 字串截取:left(str, length)
mysql> select left('starhub.com', 7);
+-----------------------------+
| left('starhub.com', 7) |
+-----------------------------+
| starhua |
+-----------------------------+
2. 字串截取:right(str, length)
mysql> select right('starhub.com', 3);
+--------------------------+
| right('starhub.com', 3) |
+--------------------------+
| com |
+--------------------------+
3. 字串截取:substring(str, pos); substring(str, pos, len)
3.1 從字串的第 4 個字元位置開始取,直到結束。
mysql> select substring('starhub.com', 4);
+-------------------------------------+
| substring('starhub.com', 4) |
+-------------------------------------+
| rhub.com |
+-------------------------------------+
3.2 從字串的第 4 個字元位置開始取,只取 2 個字元。
mysql> select substring('starhub.com', 4, 2);
+----------------------------------------+
| substring('starhub.com', 4, 2) |
+----------------------------------------+
| rh |
+----------------------------------------+
3.3 從字串的第 4 個字元位置(倒數)開始取,直到結束。
mysql> select substring('starhub.com', -4);
+--------------------------------------+
| substring('starhub.com', -4) |
+--------------------------------------+
| .com |
+--------------------------------------+
3.4 從字串的第 4 個字元位置(倒數)開始取,只取 2 個字元。
mysql> select substring('starhub.com', -4, 2);
+-----------------------------------------+
| substring('starhub.com', -4, 2) |
+-----------------------------------------+
| .c |
+-----------------------------------------+
我們注意到在函數 substring(str,pos, len)中, pos 可以是負值,但 len 不能取負值。
4. 字串截取:substring_index(str,delim,count)
4.1 截取第二個 '.' 之前的所有字元。
mysql> select substring_index('www.starhub.com.cn', '.', 2);
+------------------------------------------------------------+
| substring_index('www.starhub.com.sg', '.', 2) |
+-------------------------------------------------------------+
| www.starhub |
+-------------------------------------------------------------+
4.2 截取第二個 '.' (倒數)之後的所有字元。
mysql> select substring_index('www.starhub.com.cn', '.', -2);
+-------------------------------------------------+
| substring_index('www.starhub.com.sg', '.', -2) |
+-------------------------------------------------+
| com.sg |
+-------------------------------------------------+
4.3 如果在字串中找不到 delim 參數指定的值,就返回整個字串
mysql> select substring_index('www.starhub.com.cn', '.coc', 1);
+-----------------------------------------------------------------+
| substring_index('www.starhub.com.sg', '.con', 1) |
+------------------------------------------------------------------+
| www.starhub.com.cn |
+------------------------------------------------------------------+
5. 其他參考
更多函數和資源請參考:
http://dev.mysql.com/doc/refman/5.1/zh/functions.html
http://blog.51yip.com/mysql/965.html
http://database.51cto.com/art/200509/2479.htm