mysql SUBSTRING 字符截取函数

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 则是从后倒数,到字符串结束)

函数简介:

SUBSTRING(str,pos) , SUBSTRING(str FROM pos) SUBSTRING(str,pos,len) , SUBSTRING(str FROM pos FORlen)

不带有len 参数的格式从字符串str返回一个子字符串,起始于位置 pos。带有len参数的格式从字符串str返回一个长度同len字符相同的子字符串,起始于位置 pos。 使用 FROM的格式为标准 SQL 语法。也可能对pos使用一个负值。假若这样,则子字符串的位置起始于字符串结尾的pos 字符,而不是字符串的开头位置。在以下格式的函数中可以对pos 使用一个负值。

详情请查阅手册。

实例:

表1:user

表2:jl

期望效果:通过user表jlid字段存储的id值,读取jl表中的相应记录,这里想要读取,jl表中id为1、2的记录,首先想到用in,但是很遗憾由于

jlid字段存储的id值有2个,尽管从形式上符合in(1,2)的格式,但是如果你使用select jl.* from jl where jl.id in(select jlid from user where user.id=1)来查询的话,是不行的,他总是返回id为1的记录。

那么怎么办呢?如果我们能够分别得到1,2中的1和2就行了。好在mysql也提供了字符串截取函数SUBSTRING。

sql句法如下:

SELECT jl. *

FROM jl

WHERE jl.id = (

SELECT SUBSTRING( (

SELECT user.jlid

FROM user

WHERE user.id =1

), 1, 1 ) )

OR jl.id = (

SELECT SUBSTRING( (

SELECT user.jlid

FROM user

WHERE user.id =1

), 3, 1 )

)

LIMIT 0 , 30

简单解释一下:

SELECT SUBSTRING( (SELECT user.jlid FROM user WHERE user.id =1), 1, 1 ) )

这里用了子查询,首先查询user表中,id为1的jlid字段的值,返回的是字符串,然后使用SUBSTRING进行截取,得到字符串1

SELECT SUBSTRING( (SELECT user.jlid FROM user WHERE user.id =1), 3, 1 ) )

这条语句得到2

1和2都得到了再通过主查询的where来查询,要注意我们需要查询id=1和id=2的记录,所以用到了OR,怎么样,是不是有点麻烦,

您的第一直觉是不是要用2条sql语句,中间再配合php的explode函数来查询呢?这样想是正常的,但是这两者之间谁的效率高,站长并没有测试,希望有心的你,可以帮忙哦!

时间: 2024-07-29 20:19:45

mysql SUBSTRING 字符截取函数的相关文章

mysql sql 字符连接函数Concat Concat_ws

mysql sql 字符连接函数 1.Concat()函数     1.1 MySQL的concat函数可以连接一个或者多个字符串,如         mysql> select concat('10');         +--------------+         | concat('10') |         +--------------+         | 10           |         +--------------+        1 row in set (

thinkphp中字符截取函数msubstr()用法分析_php实例

本文实例讲述了thinkphp中字符截取函数msubstr()用法.分享给大家供大家参考,具体如下: ThinkPHP有一个内置字符截取函数msubstr()如下: msubstr($str, $start=0, $length, $charset="utf-8", $suffix=true) $str:要截取的字符串 $start=0:开始位置,默认从0开始 $length:截取长度 $charset="utf-8":字符编码,默认UTF-8 $suffix=tr

php 字符截取函数

php 字符截取函数 function sub_str($title,$lengh){  if(strlen($title)>$lengh)  {   $pp=2;   $len=strlen($title);   if($len%2<>0)   {$pp=1;}   $title=substr($title,0,$lengh-$pp);   $title=$title.' -';  }  return $title; } //------------------------------

php支持gb2312,uft-8中英文字符截取函数

php教程支持gb2312,uft-8中英文字符截取函数 <?php //截取gb2312中文字符串 function mysubstr($str, $start, $len) {     $tmps教程tr = "";     $strlen = $start + $len;     for($i = 0; $i < $strlen; $i++) {         if(ord(substr($str, $i, 1)) > 0xa0) {            

asp字符截取函数

asp字符截取函数,取字符函数 <% '定义字符串判别函数 function strlen(str) dim p_len p_len=0 strlen=0 if trim(str)<>"" then p_len=len(trim(str)) for xx=1 to p_len if asc(mid(str,xx,1))<0 then strlen=int(strlen) + 2 else strlen=int(strlen) + 1 end if next en

oralce substr字符截取函数用法

在几乎所有的数据库教程中都会有字符截取这个函数,都大同小义了,下面我们来看看在Oralce中有个数据切割函数: substr的用法吧. substr('要切割的值',从第几个位置开始切割,切割几位); 如下例: view sourceprint?substr('hello word', 3, 2) would return 'll' substr('hello word', 5) would return 'o word' substr('hello word', -3, 3) would re

javascript substring 字符截取(支持中文)代码

var url ='http://www.a.com/a.html'; var burl ='http://www.a.com/a.html#abc'; if( burl.substring(0,url.length) != url ) {  alert('不匹配'); } else {  alert(burl.substring(0,url.length)); } /* substring() 方法用于提取字符串中介于两个指定下标之间的字符. 语法 stringobject.substring

MySql中substring字符串截取函数用法

用法: SUBSTRING(str,pos,len) SUBSTRING(str FROM pos FOR len) SUBSTRING(str,pos) SUBSTRING(str FROM pos) 别名SUBSTR 截取字符串str从pos开始长度为len的字符串,如果不设置len参数默认获取pos以后的所有内容 注意字符串的索引是从1开始. 如果pos为负数则从字符串的后面开始截取. 1.截取pos后所有的数据  代码如下 复制代码 mysql> select substring('my

C#中文字符截取函数

函数|中文 ///str_value 字符///str_len 要截取的字符长度  public string leftx(string str_value,int str_len)  {   int p_num = 0;      int i;   string New_Str_value = "";    if (str_value=="")   {    New_Str_value = "";   }   else   {   int Le