0

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.

2 Answers 2

4

Use the update command

update yourtable
set age=age +1 

update yourtable
set name = 'Mr. ' + name
where gender='M'
Sign up to request clarification or add additional context in comments.

Comments

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

3 Comments

Hi Matt, Is the | symbol an equal alternative to the + symbol in Dan J's reply?
For string concatenation, or for addition? AFAIK the two are never interchangeable in SQL.
that opens up all kinds of potentials for me. I guess I can use other SQL functions there such as replace - I mean in place of the concat that is. Right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.