Retrieve 2 concatenated calculated fields : CONCAT_WS « String « SQL / MySQL






Retrieve 2 concatenated calculated fields

      
mysql>
mysql>
mysql> CREATE TABLE IF NOT EXISTS hotels
    -> (
    ->   name VARCHAR(25) PRIMARY KEY, street VARCHAR(25),
    ->   city VARCHAR(25), state VARCHAR(25), zip INT
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO hotels (name, street, city, state, zip)
    ->   VALUES ("Las Vegas Hilton", "3000 Paradise Road",
    ->          "Las Vegas", "Nevada", 89109);
Query OK, 1 row affected (0.00 sec)

mysql>
mysql> SELECT CONCAT(name, ", ", state) FROM hotels;
+---------------------------+
| CONCAT(name, ", ", state) |
+---------------------------+
| Las Vegas Hilton, Nevada  |
+---------------------------+
1 row in set (0.00 sec)

mysql> SELECT CONCAT_WS(",\n", name, street, city, state, zip)
    -> FROM hotels;
+----------------------------------------------------------------+
| CONCAT_WS(",\n", name, street, city, state, zip)               |
+----------------------------------------------------------------+
| Las Vegas Hilton,
3000 Paradise Road,
Las Vegas,
Nevada,
89109 |
+----------------------------------------------------------------+
1 row in set (0.00 sec)

mysql>
mysql> DROP TABLE hotels;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>

   
    
    
    
    
    
  








Related examples in the same category

1.CONCAT_WS() function allows you to define a separator as one of the arguments in the function
2.Use the CONCAT_WS() function to concatenate the employee names.