MySQL implementation of lastInfdexOf function example

MySQL implementation of lastInfdexOf function example

Sometimes MySQL needs to use a function similar to lastIndexOf, but it does not have a ready-made function directly available, so you need to figure it out yourself.

First, MySQL provides the following three functions:

instr(str:varchar, substr:varchar): Returns the first occurrence of subsrtr in str# For example: Find the first occurrence of b in the string ab/cd/abc.html select instr('ab/cd/abc.html', 'b');
# Unlike what the programmer thinks, the result above is 2 (the subscript starts at 1, please note)

reverse(str:varchar): reverse the string from beginning to end, input abc and output cba

length(str:varcha): Returns the length of the string

After knowing the above information, I think most people know how to implement the lastInfdexOf function.

Directly give:

select length('input string') - instr(reverse('input string'), 'string to be searched');
# eg Find the last position of '/' in the string 'ab/cd/abc.html'. The following results are: 5
select length('ab/cd/abc.html') - instr(reverse('ab/cd/abc.html'), '/');

Finally, here is a small requirement I encountered recently:

Write a SQL script to retrieve the value of the database field (the value is the path name of the file, such as ab/cd/abc.html) and parse the file name (abc.html):

select right(CONTENT_NAME, instr(reverse(CONTENT_NAME), '/') - 1) FILE_NAME
from config_template_content
where lower(CONTENT_NAME) like '%/%.html';
# This is enough when the data contains /. If the field value is abcde.html, you need to make a judgment~

The right function is used above. Please search (experience) its usefulness by yourself.

Supplement: Problems encountered when using mysql's SELECT LAST_INSERT_ID() statement

Preface:

When we insert a record into a database table whose primary key is auto-incremental (such as the user table), we expect to obtain the id of the record just written (because in some scenarios, we need to obtain this id to perform other operations). At this time, we need to use the SELECT LAST_INSERT_ID() statement to obtain the value of the auto-increment id returned after the record is inserted;

As an example:

 /**
 * Insert complaint information * @param complaintInfoModel
 * @return
 */
 @InsertProvider(type = ComplaintInfoDao.ComplaintProviderSql.class, method = "insertComplaintInfo")
 @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
 Integer insertComplaintInfo(ComplaintInfoModel complaintInfoModel); // Insert data and return the number of modified database entries

SelectKey in Mybatis is to solve the problem that the primary key is not automatically generated when inserting data. It can set the method of generating the primary key at will. But no matter how good SelectKey is, try not to encounter this situation, after all, it is very troublesome.

selectKey Attributes

property describe
keyProperty The target property to which the result of the selectKey statement should be set.
resultType

The type of result.

MyBatis can usually figure it out, but there is no problem writing it. MyBatis allows any simple type to be used as a primary key type, including strings.

order

This can be set to BEFORE or AFTER.

If set to BEFORE, then it will first select the primary key, set the keyProperty and then execute the insert statement.

If set to AFTER, the insert statement is executed first, followed by the selectKey element - this is similar to databases such as Oracle, where sequence calls can be embedded in insert statements.

statementType

As before, MyBatis supports the mapping types of STATEMENT, PREPARED and CALLABLE statements.

Represents PreparedStatement and CallableStatement types respectively.

----1. SelectKey needs to pay attention to the order attribute,

In databases that support automatic growth, such as MySQL, order needs to be set to after to get the correct value.

In the case of taking a sequence like Oracle, it needs to be set to before, otherwise an error will be reported.

-----2. In addition, when using Spring to manage transactions,

SelectKey and insert are in the same transaction, so in this case, Mysql cannot get the automatically increased Key because the data is not inserted into the database.

Cancel transaction management and there will be no problem.

The following is an example of XML and annotations. The SelectKey is very simple, and two examples are enough:

<insert id="insert" parameterType="map"> 
 insert into table1 (name) values ​​(#{name}) 
 <selectKey resultType="java.lang.Integer" keyProperty="id"> 
  CALL IDENTITY() 
 </selectKey> 
 </insert>

The input parameter of the above xml is map, and selectKey will put the result into the input parameter map. The situation with POJO is the same, but one thing to note is that the field corresponding to the keyProperty must have a corresponding setter method in the POJO, and the parameter type of the setter must be consistent, otherwise an error will be reported.

@Insert("insert into table2 (name) values(#{name})") 
@SelectKey(statement="call identity()", keyProperty="nameId", before=false, resultType=int.class) 
int insertTable2(Name name);

The above is in the form of annotation.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Mysql master-slave synchronization Last_IO_Errno:1236 error solution
  • Solution to the data asymmetry problem between MySQL and Elasticsearch
  • Solution to MySQL replication error Last_SQL_Errno:1146
  • Detailed explanation of the use of the LAST_INSERT_ID() function in Mysql

<<:  Solve the problem after adding --subnet to Docker network Create

>>:  jQuery implements simple pop-up window effect

Recommend

Basic usage of custom directives in Vue

Table of contents Preface text 1. Global Registra...

How to delete an image in Docker

The command to delete images in docker is docker ...

Vue implements multiple selections in the bottom pop-up window

This article example shares the specific code of ...

vsftpd virtual user based on MySql authentication

Table of contents 1. MySQL installation 1.2 Creat...

How to add Lua module to Nginx

Install lua wget http://luajit.org/download/LuaJI...

How to solve nginx 503 Service Temporarily Unavailable

Recently, after refreshing the website, 503 Servi...

Records of using ssh commands on Windows 8

1. Open the virtual machine and git bash window a...

In-depth understanding of React Native custom routing management

Table of contents 1. Custom routing 2. Tab naviga...

How to install MySQL via SSH on a CentOS VPS

Type yum install mysql-server Press Y to continue...

Example code of setting label style using CSS selector

CSS Selectors Setting style on the html tag can s...

The past two years with user experience

<br />It has been no more than two years sin...

Navicat remote connection to MySQL implementation steps analysis

Preface I believe that everyone has been developi...

The url value of the src or css background image is the base64 encoded code

You may have noticed that the src or CSS backgroun...