This article introduces a MySQL batch intercept the last separated part of the SQL statement to another field to have a friend in need of reference.
-
-
Field images saved data is: a.jpg;b.jpg;c.jpg
To assign the last picture of images c.jpg to Images_default
The executed SQL statement is
The code is as follows
UPDATE product SET ' image_default ' = REVERSE (MID REVERSE (' images '), 1,locate ('; ', REVERSE (Images))-1) WHERE product_id > 1
Here we take advantage of a few functions to refer to
REVERSE
REVERSE (str)
Returns the string str of the reversed character order.
The code is as follows
Mysql> Select REVERSE (' abc ');
-> ' CBA '
This function is reliable for multiple bytes.
MID
MID () function
The MID function is used to extract characters from a text field.
SQL MID () syntax
The code is as follows
SELECT MID (Column_name,start[,length]) from table_name
LOCATE
LOCATE (SUBSTR,STR), LOCATE (Substr,str,pos)
The first syntax returns the first occurrence of the string substr of the str neutron string. The second syntax returns the first occurrence of the string str neutron string substr, starting at the Pos point. If SUBSTR is not in Str, the return value is 0