w3resource logo


MySQL concat ws function

MySQL concat() function

rating has average rating 8 out of 10. Total 42 users rated.

<<PreviousNext>>

Description

MySQL CONCAT() function is used to add two or more strings. It will return non-binary strings when all the strings are non-binary and return binary string when any of the specified string is binary. A numeric argument is also converted in its equivalent binary string form. It can be avoided by using an explicit type CAST.

Syntax

CONCAT (string1, string2,…)

Arguments

Name Description
string1 First string to be joined.
string2 Second string to be joined. Up to N number of strings can be specified this way.

Pictorial representation of MySQL concat() function

pictorial representation of MySQL CONCAT function

Example of MySQL concat() function

Code

SELECT CONCAT('Use',' MySQL');

Explanation

The above MySQL statement will add two strings given in the argument and returns "Use MySQL" string.

Output

MySQL CONCAT()

Example of MySQL concat() function on columns

Sample table : publisher



Code

SELECT CONCAT(pub_city,'--> ',country)
FROM publisher; 

Explanation

The above MySQL statement will add values of pub_city column with values of country column of publisher table placing a '-->' between them.

Output

MySQL CONCAT() EXAMPLE

PHP script

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>example-concat-function - php mysql examples | w3resource</title> </head> <body> <?php echo "<h2>list of publisher's city and country with 'publisher's city--->country' format : </h2>"; echo "<table border='1' style='border-collapse: collapse;border-color: silver;'>"; echo "<tr style='font-weight: bold;'>"; echo "<td width='250' align='center'>Publisher's city</td>"; echo "</tr>"; include("../dbopen.php"); $result = mysql_query('SELECT CONCAT(pub_city,"--> ",country) FROM publisher'); while($row=mysql_fetch_array($result)) { echo "<tr>"; echo "<td align='center' width='200'>" . $row['CONCAT(pub_city,"--> ",country)'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> </body> </html>

View the example in browser

photo credit: axe. Photo is used under creative Common License.

<<PreviousNext>>

Rate this tutorial


Your Rating: not set

Share this tutorial

RSS Feed