33 Votes

MySQL: Read values from two columns and output them in a single variable

Tip by Computer Expert | Last update on 2021-03-08 | Created on 2012-07-02

An interesting MySQL command allows us to read values from two or more columns to output them along with any other string in only one single variable, just like as if the values are coming from one separate column. The command is called CONCAT.

An Example

As an example, we have two columns: one is called "firstname" and the other is called "lastname". We now want to read out the first and last names together and put a space between them. The MySQL query with CONCAT is the following:

SELECT CONCAT(firstname, ' ', lastname) FROM tab WHERE id=100;

As a result we get, for example, "Anna Backer" and we do not have to worry about joining the two strings "firstname" and "lastname" after reading the values from our database.

ReplyPositiveNegative

About the Author

AvatarThe author has not added a profile short description yet.
Show Profile

 

Related Topics

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.