通过添加带有MySQL查询的char来更新所有列值

时间:2021-04-20 08:03:31

I have a table with column for storing product code.

我有一个用于存储产品代码的列表。

   | product_id | Product-name | product_code|
   |    1       | soap         | QTI4589     |
        2         abc            DRT45869
        3         xyz            IND458

I want to update all values under column product_code with QTY-4589 , DRT-45869, IND-458 ...

我想用QTY-4589,DRT-45869,IND-458更新列product_code下的所有值...

means want to put "-" between code and number.

意味着想在代码和数字之间加上“ - ”。

Tried with replace() mysql function but not working. Please help me how to do this.

尝试使用replace()mysql函数但不能正常工作。请帮我怎么做。

3 个解决方案

#1


0  

Did you try the INSERT(str,pos,len,newstr) mysql function ???

你有没有尝试过INSERT(str,pos,len,newstr)的mysql函数?

UPDATE table_name SET product_code= INSERT (product_code,4,1,"-") WHERE 1

#2


2  

You can do with some mysql functions CONCAT() , LEFT() , MID() , LENGTH()

你可以用一些mysql函数CONCAT(),LEFT(),MID(),LENGTH()

Try below :

试试以下:

UPDATE table SET product_code=CONCAT(LEFT(product_code,3),'-',MID(product_code,4,LENGTH(product_code)))

#3


1  

Is the code always three chars? if so you can use

代码总是三个字符?如果是这样你可以使用

UPDATE `table` SET `product_code` = CONCAT( 
    SUBSTRING( product_code, 0, 3 ),
    '-',
    SUBSTRING( product_code FROM 4 )
)

#1


0  

Did you try the INSERT(str,pos,len,newstr) mysql function ???

你有没有尝试过INSERT(str,pos,len,newstr)的mysql函数?

UPDATE table_name SET product_code= INSERT (product_code,4,1,"-") WHERE 1

#2


2  

You can do with some mysql functions CONCAT() , LEFT() , MID() , LENGTH()

你可以用一些mysql函数CONCAT(),LEFT(),MID(),LENGTH()

Try below :

试试以下:

UPDATE table SET product_code=CONCAT(LEFT(product_code,3),'-',MID(product_code,4,LENGTH(product_code)))

#3


1  

Is the code always three chars? if so you can use

代码总是三个字符?如果是这样你可以使用

UPDATE `table` SET `product_code` = CONCAT( 
    SUBSTRING( product_code, 0, 3 ),
    '-',
    SUBSTRING( product_code FROM 4 )
)