oralce函数substr和mysql函数substring_index小记 ???? 最近偶尔在mysql数据库中看到了substring_index函数,先简单介绍下mysql的substring_index函数。??? substring_index(str,delim,count) 返回字符串str中在第 count 个出现的分隔符 delim之前的子串,和ja
oralce函数substr和mysql函数substring_index小记???? 最近偶尔在mysql数据库中看到了substring_index函数,先简单介绍下mysql的substring_index函数。???
substring_index(str,delim,count) 返回字符串str中在第 count 个出现的分隔符 delim之前的子串,和java中的str.substring函数类似。 如果count是正数时候,substring_index是从左到右的顺序检查分隔符delim所在的位置。 如果count是负数时候,substring_index是从右到左的顺序检查分隔符delim所在的位置,返回的是从字符串str左边下标1开始的下标位置。 特殊点如果count等于0,那么,不管delim是否在str中存在,你得到的将是一个空白字符串。如下所示
???
select substring_index('www.baidu.com', '.', 0); select substring_index('www.baidu.com', '-', 0);
??? 上面说的很抽象,来个简单的例子。
???
select substring_index('www.baidu.com', '.', 2);
?? 2为正数,.在str中存在,返回的结果是从下标1到第2次.出现的位置之间substr(1,10-1)的子串。所以结果是'www.baidu'。
??
select substring_index('www.baidu.com', '.', -2);
??? -2为负数,.在str中存在,.在str中搜索顺序是从右到左第2次出现的位置是4,用于是负数返回的是从最后到4+1的子串,也就是substr(str,4+1),所以结果是baidu.com。
??? 可以看到substring_index返回的子串是不包含分隔符 delim在str中第count次匹配的位置的,如果分隔符delim在str中出现的位置小于count次会发生什么呢?来测试下:
??
select substring_index('www.baidu.com', '.', 3); select substring_index('www.baidu.com', '.', -3);
?? 运行之后可以看到返回的结果都是'www.baidu.com'。再来测试delim在str中不存在的情况:
??
select substring_index('www.baidu.com', '-', -1); select substring_index('www.baidu.com', '-',1);
??? 运行之后可以看到返回的结果都是'www.baidu.com',从上面的测试可以知道分隔符delim在str中不存在或者出现次数小于指定的count时将返回str本身。上面只是测试了英文数据,中文数据测试也是一样的,测试如下:
??
select substring_index('我是中文_测试_数据','_',2) from dual;--我是中文_测试 select substring_index('我是中文_测试_数据','_',-2) from dual;--测试_数据 select substring_index('我是中文_测试_数据','_',0) from dual;--空白字符串 select substring_index('我是中文_测试_数据','_',3) from dual;--我是中文_测试_数据
??? 中英文混合的情况我就不测试了。
??? mysql有substring_index函数,oracle是否也要类似的函数呢?
我所知道的oracle中常见的只有substr和mysql的substring_index函数类似,有网友知道oralce其他函数的,麻烦留言告知。mysql中也有substr,关于两者的区别,后面我会简述。
??? 先简单的介绍下oracle的substr函数和instr函数,后面我会用这2个函数模拟mysql的substring_index函数
???
instr( string1, string2 [, start_position [, nth_appearance ] ] ) string1 源字符串 string2 目标字符串. start_position 从string1 的哪个位置开始查找。默认为1. 字符串索引从1开始。为正,从左到右开始检索,为负,从右到左检索。 nth_appearance 代表要查找第几次出现的string2. 此参数可选,默认为 1.不能为负数 返回要查找的字符串string2在源字符串string1中的符合条件的开始索引
???
substr(string,start_position,length) string 源字符串,即被截取的字符串. start_position 字符截取的开始位置.start_position大于0时,从左边算起,小于0时,从右边查起 length 截取字符的个数.默认截取到最后一位.
??? 先来个测试:
???
select instr('www.baidu.com', '.',-1,2) from dual
??? .在str中存在,-1说明检索是从最后一位开始,2说明出现2次,返回的结果是4
??
select instr('www.baidu.com', '.',1,2) from dual
??? .在str中存在,1说明按从左到右的顺序,2说明出现2次,返回的结果是10
??? 下面来点特殊的测试,先测试nth_appearance,不是说不能为负数吗,我测试nth_appearance=0看下
???
select instr('www.baidu.com', '.',1,0) from dual select instr('www.baidu.com', '.',-1,0) from dual
??? 你们猜结果是什么?结果是ORA-01428:参数'0'超出范围,说明nth_appearance不能为0,只能大于0。
??? 再测试start_position,令start_position=0看下
???
select instr('www.baidu.com', '.',0,1) from dual select instr('www.baidu.com', '-',0,1) from dual select instr('www.baidu.com', '.',0,2) from dual select instr('www.baidu.com', '-',0,2) from dual
??? 测试结果都是0;如果把start_position改为>0,结果如下
???
select instr('www.baidu.com', '.',1,1) from dual--4 select instr('www.baidu.com', '-',1,1) from dual--0 select instr('www.baidu.com', '.',1,2) from dual--10 select instr('www.baidu.com', '-',1,2) from dual--0
??? 如果把start_position改为
???
select instr('www.baidu.com', '.',-1,1) from dual--10 select instr('www.baidu.com', '-',-1,1) from dual--0 select instr('www.baidu.com', '.',-1,2) from dual--4 select instr('www.baidu.com', '-',-1,2) from dual--0 select instr('www.baidu.com', '.',-1,3) from dual--0 select instr('www.baidu.com', '-',-1,3) from dual--0 select instr('www.baidu.com', '.',-1,3) from dual--0 select instr('www.baidu.com', '-',-1,3) from dual--0
??? 可以看出instr函数目标字符串在str中不存在时候或者出现次数小于给定的次数时,返回的都是0;
??? 使用中文测试也是一样:
???
select instr('我是中文_测试_数据','_',1,0) from dual;--ORA-01428:参数'0'超出范围 select instr('我是中文_测试_数据','_',1,2) from dual;--8 select instr('我是中文_测试_数据','_',1,3) from dual;--0 select instr('我是中文_测试_数据','_',-1,2) from dual;--5 select instr('我是中文_测试_数据','_',-1,3) from dual;--0
??? 下面测试下substr。
???
select substr('www.baidu.com',0,4) from dual select substr('www.baidu.com',1,4) from dual
??? 上面2个执行结果都是'www.'。如果上面是在mysql下测试,结果如下:
???
select substr('www.baidu.com',0,4) from dual--空白字符串 select substr('www.baidu.com',1,4) from dual--www.
??? 从这里可以看出mysql中substr开始位置不能为0,而oracle下从0开始或者从1开始结果是一样的,下面继续oracle测试。
??
select substr('www.baidu.com',1) from dual select substr('www.baidu.com',1,22) from dual
??? 返回结果是'www.baidu.com'。
??
select substr('www.baidu.com',-1) from dual select substr('www.baidu.com',-1,4) from dual select substr('www.baidu.com',-1,22) from dual
??? 返回的结果是'm',说明substr返回的是从开始位置到str最后一位。
??? 使用中文测试如下:
???
select substr('我是中文_测试_数据',1) from dual--我是中文_测试_数据 select substr('我是中文_测试_数据',1,22) from dual--我是中文_测试_数据 select substr('我是中文_测试_数据',-1) from dual--据 select substr('我是中文_测试_数据',-1,4) from dual--据 select substr('我是中文_测试_数据',-1,22) from dual--据
??? 从上面的测试可以看出oracle下instr函数和mysql的substring_index函数很像,instr函数已经可以得到下标了,结合substr可以截取子串返回。
??? Mysql的
???
select substring_index('www.baidu.com', '.', 2);
??? Oralce可以这样做:
???
select substr('www.baidu.com', 1, instr('www.baidu.com', '.',1,2)-1) from dual
??? instr函数再-1是因为mysqlsubstring_index返回结果不包括.,上面返回的结果都是'www.baidu'。
???
??? Mysql的
???
select substring_index('www.baidu.com', '.', -2);
??? Oracle可以这样做:
???
select substr('www.baidu.com', instr('www.baidu.com', '.',-1,2) + 1, length('www.baidu.com'))from dual select substr('www.baidu.com', instr('www.baidu.com', '.',-1,2) + 1) from dual
??? 上面可以不使用length函数,因为oracle的substr默认截取到str最后一位。
???? 中文测试如下:
???? Mysql下面:
????
select substring_index('我是中文_测试_数据','_',1) from dual;--我是中文 select substring_index('我是中文_测试_数据','_',2) from dual;--我是中文_测试 select substring_index('我是中文_测试_数据','_',-2) from dual;--测试_数据 select substring_index('我是中文_测试_数据','_',0) from dual;--空白字符串 select substring_index('我是中文_测试_数据','_',3) from dual;--我是中文_测试_数据 select substring_index('我是中文_测试_数据','.',3) from dual;--我是中文_测试_数据
??? Oracle下面:
???
select substr('我是中文_测试_数据', 1, instr('我是中文_测试_数据', '_',1,1)-1) from dual--我是中文 select substr('我是中文_测试_数据', 1, instr('我是中文_测试_数据', '_',1,2)-1) from dual--我是中文_测试 select substr('我是中文_测试_数据', instr('我是中文_测试_数据', '_',-1,2) + 1) from dual--测试_数据 select substr('我是中文_测试_数据', 1, instr('我是中文_测试_数据', '_',1,0)-1) from dual--ORA-01428:参数'0'超出范围 select substr('我是中文_测试_数据', 1, instr('我是中文_测试_数据', '_',1,3)-1) from dual--空白 select substr('我是中文_测试_数据', 1, instr('我是中文_测试_数据', '.',1,3)-1) from dual--空白
??? 可以看到我写的这个有点问题,如果想在nth_appearance(>0)大于目标串在str中出现的次数时和mysql一样返回整个字符串,可以这样写:
???
select substr('我是中文_测试_数据', 1, (select (case when instr('我是中文_测试_数据', '_', 1, 3) <= 1 then (select length('我是中文_测试_数据') from dual) else (instr('我是中文_测试_数据', '_', 1, 3) - 1) end) from dual)) from dual
??? 这个在nth_appearance=0时用不了,进一步,考虑到nth_appearance=0时候返回NULL可以这样做,返回''这样的字符串使用substr做不了。
???
select substr('我是中文_测试_数据', 1, (select (case when 0 = 0 then -1 else case when instr('我是中文_测试_数据', '_', 1, 3) <= 1 then (select length('我是中文_测试_数据') from dual) else (instr('我是中文_测试_数据', '_', 1, 3) - 1) end end) from dual)) from dual
??? 上面的sql是不是很丑陋,,我们可以定义一个存储过程封装一下。
????
create or replace procedure my_substring_index(str in varchar2, delim in varchar2, in_count in int, out_str out varchar2) as v_time int; v_delim varchar2(20); begin if str is null then out_str := '空白字符串'; elsif length(str) = 0 then out_str := '空白字符串'; end if; if delim is null then v_delim := ' '; elsif length(delim) = 0 then v_delim := ' '; else v_delim := delim; end if; if in_count = 0 then out_str := '空白字符串'; elsif in_count>0 then v_time := instr(str, delim, 1, in_count); else v_time := instr(str, delim, -1, (-1)*in_count); end if; if v_time = 0 then out_str := str; end if; if in_count>0 then if v_time = 1 then out_str := '空白字符串'; elsif v_time > 1 then select substr(str, 1, v_time - 1) into out_str from dual; end if; elsif in_count<0 then if v_time >= 1 then select substr(str, v_time + 1) into out_str from dual; end if; end if; end my_substring_index;
?? Oracle上面测试过程如下:
??? 在My Object下面的Procedures下找到存储过程my_substring_index,点击右键选择Test.出来Oracle的测试脚本:
???
begin -- Call the procedure my_substring_index(str => :str, delim => :delim, in_count => :in_count, out_str => :out_str); end;
?? 在测试脚本页面下面填上具体值,按F8,不出意外的化可以看到返回值。
?? 如果想自己写脚本可以这样,新建一个sql文件,内容如下:
??
declare v_str varchar2(50); v_delim varchar2(20); v_in_count number; v_out_str varchar2(20); begin v_str:='&str'; v_delim:='&delim'; v_in_count:=&in_count; my_substring_index(str => v_str, delim => v_delim, in_count => v_in_count, out_str => v_out_str); dbms_output.put_line('result='||v_out_str); end;
??? 在命令行下运行:
???
SQL> set serveroutput on; SQL> start f:/saveFile/test.sql 16 / result=www.baidu PL/SQL procedure successfully completed
??? 如果想直接在cmd window下测试,可以这样:
???
SQL> set serveroutput on; SQL> var v_str varchar2(50); SQL> var v_delim varchar2(20); SQL> var v_in_count number; SQL> var v_out_str varchar2(50); SQL> exec :v_str:='我是中文_测试_数据'; SQL> exec :v_delim:='_'; SQL> exec :v_in_count:=2; SQL> exec my_substring_index(:v_str,:v_delim,:v_in_count,:v_out_str); SQL> print v_out_str;
????? 以上是我写的oralce函数substr和mysql函数substring_index简单介绍,文章有点简单,但毕竟是原创,转载请注明出处。
????? 全文完,写的不好的地方请见谅,大神请绕过。
?
?
?
?
?
?
?
?
?
?
?
?
??
????

innodbbufferpool은 데이터와 인덱싱 페이지를 캐싱하여 디스크 I/O를 줄여 데이터베이스 성능을 향상시킵니다. 작업 원칙에는 다음이 포함됩니다. 1. 데이터 읽기 : BufferPool의 데이터 읽기; 2. 데이터 작성 : 데이터 수정 후 BufferPool에 쓰고 정기적으로 디스크로 새로 고치십시오. 3. 캐시 관리 : LRU 알고리즘을 사용하여 캐시 페이지를 관리합니다. 4. 읽기 메커니즘 : 인접한 데이터 페이지를 미리로드합니다. Bufferpool을 크기를 조정하고 여러 인스턴스를 사용하여 데이터베이스 성능을 최적화 할 수 있습니다.

다른 프로그래밍 언어와 비교할 때 MySQL은 주로 데이터를 저장하고 관리하는 데 사용되는 반면 Python, Java 및 C와 같은 다른 언어는 논리적 처리 및 응용 프로그램 개발에 사용됩니다. MySQL은 데이터 관리 요구에 적합한 고성능, 확장 성 및 크로스 플랫폼 지원으로 유명하며 다른 언어는 데이터 분석, 엔터프라이즈 애플리케이션 및 시스템 프로그래밍과 같은 해당 분야에서 이점이 있습니다.

MySQL은 데이터 저장, 관리 및 분석에 적합한 강력한 오픈 소스 데이터베이스 관리 시스템이기 때문에 학습 할 가치가 있습니다. 1) MySQL은 SQL을 사용하여 데이터를 작동하고 구조화 된 데이터 관리에 적합한 관계형 데이터베이스입니다. 2) SQL 언어는 MySQL과 상호 작용하는 열쇠이며 CRUD 작업을 지원합니다. 3) MySQL의 작동 원리에는 클라이언트/서버 아키텍처, 스토리지 엔진 및 쿼리 최적화가 포함됩니다. 4) 기본 사용에는 데이터베이스 및 테이블 작성이 포함되며 고급 사용량은 Join을 사용하여 테이블을 결합하는 것과 관련이 있습니다. 5) 일반적인 오류에는 구문 오류 및 권한 문제가 포함되며 디버깅 기술에는 구문 확인 및 설명 명령 사용이 포함됩니다. 6) 성능 최적화에는 인덱스 사용, SQL 문의 최적화 및 데이터베이스의 정기 유지 보수가 포함됩니다.

MySQL은 초보자가 데이터베이스 기술을 배우는 데 적합합니다. 1. MySQL 서버 및 클라이언트 도구를 설치하십시오. 2. SELECT와 같은 기본 SQL 쿼리를 이해하십시오. 3. 마스터 데이터 작업 : 데이터를 만들고, 삽입, 업데이트 및 삭제합니다. 4. 고급 기술 배우기 : 하위 쿼리 및 창 함수. 5. 디버깅 및 최적화 : 구문 확인, 인덱스 사용, 선택*을 피하고 제한을 사용하십시오.

MySQL은 테이블 구조 및 SQL 쿼리를 통해 구조화 된 데이터를 효율적으로 관리하고 외래 키를 통해 테이블 간 관계를 구현합니다. 1. 테이블을 만들 때 데이터 형식을 정의하고 입력하십시오. 2. 외래 키를 사용하여 테이블 간의 관계를 설정하십시오. 3. 인덱싱 및 쿼리 최적화를 통해 성능을 향상시킵니다. 4. 데이터 보안 및 성능 최적화를 보장하기 위해 데이터베이스를 정기적으로 백업 및 모니터링합니다.

MySQL은 웹 개발에 널리 사용되는 오픈 소스 관계형 데이터베이스 관리 시스템입니다. 주요 기능에는 다음이 포함됩니다. 1. 다른 시나리오에 적합한 InnoDB 및 MyISAM과 같은 여러 스토리지 엔진을 지원합니다. 2.로드 밸런싱 및 데이터 백업을 용이하게하기 위해 마스터 슬레이브 복제 기능을 제공합니다. 3. 쿼리 최적화 및 색인 사용을 통해 쿼리 효율성을 향상시킵니다.

SQL은 MySQL 데이터베이스와 상호 작용하여 데이터 첨가, 삭제, 수정, 검사 및 데이터베이스 설계를 실현하는 데 사용됩니다. 1) SQL은 Select, Insert, Update, Delete 문을 통해 데이터 작업을 수행합니다. 2) 데이터베이스 설계 및 관리에 대한 생성, 변경, 삭제 문을 사용하십시오. 3) 복잡한 쿼리 및 데이터 분석은 SQL을 통해 구현되어 비즈니스 의사 결정 효율성을 향상시킵니다.

MySQL의 기본 작업에는 데이터베이스, 테이블 작성 및 SQL을 사용하여 데이터에서 CRUD 작업을 수행하는 것이 포함됩니다. 1. 데이터베이스 생성 : createAbasemy_first_db; 2. 테이블 만들기 : CreateTableBooks (idintauto_incrementprimarykey, titlevarchar (100) notnull, authorvarchar (100) notnull, published_yearint); 3. 데이터 삽입 : InsertIntobooks (Title, Author, Published_year) VA


핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

ZendStudio 13.5.1 맥
강력한 PHP 통합 개발 환경

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

mPDF
mPDF는 UTF-8로 인코딩된 HTML에서 PDF 파일을 생성할 수 있는 PHP 라이브러리입니다. 원저자인 Ian Back은 자신의 웹 사이트에서 "즉시" PDF 파일을 출력하고 다양한 언어를 처리하기 위해 mPDF를 작성했습니다. HTML2FPDF와 같은 원본 스크립트보다 유니코드 글꼴을 사용할 때 속도가 느리고 더 큰 파일을 생성하지만 CSS 스타일 등을 지원하고 많은 개선 사항이 있습니다. RTL(아랍어, 히브리어), CJK(중국어, 일본어, 한국어)를 포함한 거의 모든 언어를 지원합니다. 중첩된 블록 수준 요소(예: P, DIV)를 지원합니다.

에디트플러스 중국어 크랙 버전
작은 크기, 구문 강조, 코드 프롬프트 기능을 지원하지 않음

드림위버 CS6
시각적 웹 개발 도구
