w3resource logo

MySQL concat ws function

MySQL CONCAT() function

Secondary Nav

CONCAT() function

MySQL CONCAT() function is used to add two or more strings.

  • There may be one or more arguments.
  • Returns the string that results from concatenating the arguments.
  • Returns a nonbinary string, if all arguments are nonbinary strings.
  • Returns a binary string, if the arguments include any binary strings.
  • If the argument is numeric, it is converted to its equivalent nonbinary string form.
  • Returns NULL if any argument is NULL.

 Try queries by yourself after completing this tutorial.

Syntax :

CONCAT (string1, string2,…)


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.

MySQL Version : 5.6

Video Presentation

Examples of MySQL CONCAT() function

One argument :

mysql> SELECT CONCAT('w3resource');
| CONCAT('w3resource') |
| w3resource           |
1 row in set (0.00 sec)

Two or more arguments :

mysql> SELECT CONCAT('w3resource','.','com');
| CONCAT('w3resource','.','com') |
| w3resource.com                 |
1 row in set (0.00 sec)

One of the arguments is NULL :

mysql> SELECT CONCAT('w3resource','.','com',NULL);
| CONCAT('w3resource','.','com',NULL) |
| NULL                                |
1 row in set (0.02 sec)

Numeric argument :

mysql> SELECT CONCAT(102.33);
| CONCAT(102.33) |
| 102.33         |
1 row in set (0.00 sec)

For quoted strings, concatenation can be performed by placing the strings next to each other :

mysql> SELECT 'w3resource' '.' 'com';
| w3resource     |
| w3resource.com | 
1 row in set (0.00 sec)

Pictorial representation of MySQL CONCAT() function

pictorial representation of MySQL CONCAT function

Example of MySQL CONCAT() function on columns

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

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

Sample table : publisher

Output :

mysql> SELECT CONCAT(pub_city,'--> ',country)
    -> FROM publisher;
| CONCAT(pub_city,'--> ',country) |
| New York--> USA                 | 
| Mumbai--> India                 | 
| Adelaide--> Australia           | 
| London--> UK                    | 
| Houstan--> USA                  | 
| New York--> USA                 | 
| Cambridge--> UK                 | 
| New Delhi--> India              | 
8 rows in set (0.00 sec)

PHP script

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>example-concat-function - php mysql examples | w3resource</title>
<meta name="description" content="example-concat-function - php mysql examples | w3resource">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<div class="container">
<div class="row">
<div class="col-md-12">
<h2>list of publisher's city and country with 'publisher's city--->country' format:</h2>
<table class='table table-bordered'>
<th>Publisher's City</th>
$db = "your_dbname";
$dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);
foreach($dbh->query('SELECT CONCAT(pub_city,"--> ",country)
FROM publisher') as $row) {
echo "<tr>";
echo "<td>" . $row['CONCAT(pub_city,"--> ",country)'] . "</td>";
echo "</tr>";

View the example in browser

JSP script

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.io.*" %>
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
try {
String Host = "jdbc:mysql://localhost:3306/w3resour_bookinfo";
Connection connection = null;
Statement statement = null;
ResultSet rs = null;
connection = DriverManager.getConnection(Host, "root", "datasoft123");
statement = connection.createStatement();
String Data ="SELECT CONCAT(pub_city,'--> ',country) FROM publisher";
rs = statement.executeQuery(Data);
<TABLE border="1">
<tr width="10" bgcolor="#9979">
<td>Publisher's City</td>
while (rs.next()) {
<TD><%=rs.getString("CONCAT(pub_city,'--> ',country)")%></TD>
<%   }    %>
} catch (Exception ex) {
out.println("Cant connect to database.");

Try the following Queries

      Write a SQL statement to display the publisher city and name according to the group on publisher city.

      Write a SQL statement to display the publisher city and name and country office with a suitable title for those publishers which country office and publishing city are in the same place.

       Write a SQL statement to display the publisher name, country office and a maximum number of branches with the suitable title for those publishers who maintain on and above 15 branches worldwide.

Sample table : publisher

MySQL Online Editor

 Want to practice more MySQL exercises? Click here to get three hundred plus exercises with solutions.



All String Functions

MySQL String Functions, slide presentation