Friday, December 13, 2024
HomeDatabaseMySQLMySQL: Use CONCAT to include text in SELECT results

MySQL: Use CONCAT to include text in SELECT results

The CONCAT function in MySQL allows SELECT statements to act like printf commands by mingling text and query results together.


For example, to generate a quick list of links in HTML from a table ‘links’ with the columns ‘url’ (https://www.tech-recipes.com) and ‘title’ (Tech-Recipes) so that the output looks like:

Tech-Recipes

use a select statement like:

SELECT CONCAT('<a href="', url, '">', title, '</a><br />') FROM links;

The CONCAT function simply joins the comma-separated list of strings and expressions (in this case column results from the query: url and title). As with any SELECT statement, the result set can be varied with the use of an optional WHERE clause and other valid SQL syntax.

Quinn McHenry
Quinn McHenry
Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

LATEST REVIEWS

Recent Comments

error: Content is protected !!