MySQL field value (string) Accumulation

Source: Internet
Author: User

When updating records, mysql must add new values on the original values.

For example, there was a record:

Id Country
A784829a-c0dc-4cb6-88a9-8c376fab83a6 USA

 

 

 

Update the country field now: Add "$ + $ UK" based on the original value to change it to the following value:

Id Country
A784829a-c0dc-4cb6-88a9-8c376fab83a6 USA $ + $ UK

 

 

However, when updating, you do not know the original value of the record, nor do you want to obtain the original value through mysql queries (in fact, you do not care about the original value, you only need to add a new value based on the original value ).

 

Finally, use the following SQL statement:

insert into video(id, country) values('a784829a-c0dc-4cb6-88a9-8c376fab83a6', 'USA') on duplicate key update country=CONCAT(country,values(country))

 

 

Bytes --------------------------------------------------------------------------------------------

Update:

If the original value is NULL, the preceding SQL statement cannot update the field value (but mysql does not report an error ).

Id Country
A784829a-c0dc-4cb6-88a9-8c376fab83a6 NULL

 

 

That is, after the preceding SQL statement is executed for the preceding records, the country value is NULL;

 

However, if the value is "" (NULL String) rather than NULL, the preceding SQL statement can run normally:

Id Country
A784829a-c0dc-4cb6-88a9-8c376fab83a6  

 

 

After the preceding SQL statement is executed, country = "USA ".

 

Conclusion: when creating table, you must specify this field not null and set default.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.