简体   繁体   中英

updating all column values by adding a char with MySQL query

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 ...

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

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

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

Try below :

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

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 )
)

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM