oralce函数substr和mysql函数substring_index初记
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函数。???
1 2 3 4 |
|
???
1 2 |
|
??? 上面说的很抽象,来个简单的例子。
???
1 |
|
?? 2为正数,.在str中存在,返回的结果是从下标1到第2次.出现的位置之间substr(1,10-1)的子串。所以结果是'www.baidu'。
??
1 |
|
??? -2为负数,.在str中存在,.在str中搜索顺序是从右到左第2次出现的位置是4,用于是负数返回的是从最后到4+1的子串,也就是substr(str,4+1),所以结果是baidu.com。
??? 可以看到substring_index返回的子串是不包含分隔符 delim在str中第count次匹配的位置的,如果分隔符delim在str中出现的位置小于count次会发生什么呢?来测试下:
??
1 2 |
|
?? 运行之后可以看到返回的结果都是'www.baidu.com'。再来测试delim在str中不存在的情况:
??
1 2 |
|
??? 运行之后可以看到返回的结果都是'www.baidu.com',从上面的测试可以知道分隔符delim在str中不存在或者出现次数小于指定的count时将返回str本身。上面只是测试了英文数据,中文数据测试也是一样的,测试如下:
??
1 2 3 4 |
|
??? 中英文混合的情况我就不测试了。
??? mysql有substring_index函数,oracle是否也要类似的函数呢?
我所知道的oracle中常见的只有substr和mysql的substring_index函数类似,有网友知道oralce其他函数的,麻烦留言告知。mysql中也有substr,关于两者的区别,后面我会简述。
??? 先简单的介绍下oracle的substr函数和instr函数,后面我会用这2个函数模拟mysql的substring_index函数
???
1 2 3 4 5 6 |
|
???
1 2 3 4 |
|
??? 先来个测试:
???
1 |
|
??? .在str中存在,-1说明检索是从最后一位开始,2说明出现2次,返回的结果是4
??
1 |
|
??? .在str中存在,1说明按从左到右的顺序,2说明出现2次,返回的结果是10
??? 下面来点特殊的测试,先测试nth_appearance,不是说不能为负数吗,我测试nth_appearance=0看下
???
1 2 |
|
??? 你们猜结果是什么?结果是ORA-01428:参数'0'超出范围,说明nth_appearance不能为0,只能大于0。
??? 再测试start_position,令start_position=0看下
???
1 2 3 4 |
|
??? 测试结果都是0;如果把start_position改为>0,结果如下
???
1 2 3 4 |
|
??? 如果把start_position改为<0,结果如下:
???
1 2 3 4 5 6 7 8 |
|
??? 可以看出instr函数目标字符串在str中不存在时候或者出现次数小于给定的次数时,返回的都是0;
??? 使用中文测试也是一样:
???
1 2 3 4 5 |
|
??? 下面测试下substr。
???
1 2 |
|
??? 上面2个执行结果都是'www.'。如果上面是在mysql下测试,结果如下:
???
1 2 |
|
??? 从这里可以看出mysql中substr开始位置不能为0,而oracle下从0开始或者从1开始结果是一样的,下面继续oracle测试。
??
1 2 |
|
??? 返回结果是'www.baidu.com'。
??
1 2 3 |
|
??? 返回的结果是'm',说明substr返回的是从开始位置到str最后一位。
??? 使用中文测试如下:
???
1 2 3 4 5 |
|
??? 从上面的测试可以看出oracle下instr函数和mysql的substring_index函数很像,instr函数已经可以得到下标了,结合substr可以截取子串返回。
??? Mysql的
???
1 |
|
??? Oralce可以这样做:
???
1 |
|
??? instr函数再-1是因为mysqlsubstring_index返回结果不包括.,上面返回的结果都是'www.baidu'。
???
??? Mysql的
???
1 |
|
??? Oracle可以这样做:
???
1 2 |
|
??? 上面可以不使用length函数,因为oracle的substr默认截取到str最后一位。
???? 中文测试如下:
???? Mysql下面:
????
1 2 3 4 5 6 |
|
??? Oracle下面:
???
1 2 3 4 5 6 |
|
??? 可以看到我写的这个有点问题,如果想在nth_appearance(>0)大于目标串在str中出现的次数时和mysql一样返回整个字符串,可以这样写:
???
1 2 3 4 5 6 7 8 9 10 |
|
??? 这个在nth_appearance=0时用不了,进一步,考虑到nth_appearance=0时候返回NULL可以这样做,返回''这样的字符串使用substr做不了。
???
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
??? 上面的sql是不是很丑陋,,我们可以定义一个存储过程封装一下。
????
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
?? Oracle上面测试过程如下:
??? 在My Object下面的Procedures下找到存储过程my_substring_index,点击右键选择Test.出来Oracle的测试脚本:
???
1 2 3 4 5 6 7 |
|
?? 在测试脚本页面下面填上具体值,按F8,不出意外的化可以看到返回值。
?? 如果想自己写脚本可以这样,新建一个sql文件,内容如下:
??
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
??? 在命令行下运行:
???
1 2 3 4 5 6 |
|
??? 如果想直接在cmd window下测试,可以这样:
???
1 2 3 4 5 6 7 8 9 10 |
|
????? 以上是我写的oralce函数substr和mysql函数substring_index简单介绍,文章有点简单,但毕竟是原创,转载请注明出处。
????? 全文完,写的不好的地方请见谅,大神请绕过。
?
?
?
?
?
?
?
?
?
?
?
?
??
????

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.
