简体   繁体   中英

how do you increment a field in mysql?

Say, you got a table of 100 records. And field age contains a some integers. And you want all those integers to be incremented by 1.

or you got a textfield called name and a bunch of names in there. And you want all those names to be prefixed as Mr. .

Is there a way to achieve this in one SQL command?

The alternative would be to compile a recordset of these 100 recs and going thru a loop and then running an individual update statement.

Use the update command

update yourtable
set age=age +1 

update yourtable
set name = 'Mr. ' + name
where gender='M'
  1. UPDATE mytable SET age = age+1
  2. UPDATE mytable SET name = CONCAT('Mr. ', name )

If MySQL is in ANSI mode – specifically, PIPES_AS_CONCAT , you can use 'Mr. ' || name 'Mr. ' || name 'Mr. ' || name instead.

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