Concatenation – such a wonderful word to say. Also a real get out of jail card when manipulating data in your MySQL database.

Take a scenario where you want to create a ‘fullname’ field from a ‘firstname’ and ‘surname’ field. This can be achieved with the CONCAT command.

The syntax is

CONCAT('string1', 'string2', 'string3') etc

A SQL SELECT statement could be produced such as

SELECT CONCAT(firstname, ' ', surname)
AS fullname FROM staff;

This would concatenate the ‘first name’ and ‘surname’ fields together with a white space.

You can also use CONCAT with UPDATE statements to amend records. For example the following would append ‘Mr’ to all the firstname fields that were flagged as Male.

UPDATE staff SET first name = CONCAT('Mr ', first name)
WHERE gender = 'M';

Hope this provides an excuse for dropping the odd concatenation into your casual conversation.

Leave a Comment