Share on Google+Share on Google+

MySQL Append String

Advertisement
This example illustrates how to append string of different column.

MySQL Append String

This example illustrates how to append string of different column.

In this example we create a table 'mca' with 'studentid', 'sectionid', 'courseid' and 'time'. In the table we insert value by using 'INSERT' query. The 'SELECT' query is used to append value of 'time' and 'sectionid' which display in the output below.

Query

CREATE TABLE mca (
    studentid  INT NOT NULL,
    sectionid  INT NOT NULL,
    courseid   INT NOT NULL PRIMARY KEY,
    time       VARCHAR(50)
 );

Query

INSERT INTO mca (studentid,sectionid,courseid,time) 
   VALUES (1,1,6,'Mon 09:30-11:00');
INSERT INTO mca (studentid,sectionid,courseid,time) 
    VALUES (2,2,5,'Mon 11:30-12:00, Thu 09:00-11:00');
INSERT INTO mca (studentid,sectionid,courseid,time) 
   VALUES (3,3,3,'Mon 14:30-16:00');

Query

SELECT studentid, CONCAT(time, ', section ', sectionid) 
    AS ClassDetails FROM mca;

Output

+-----------+---------------------------------------------+
| studentid | ClassDetails                                |
+-----------+---------------------------------------------+
| 3         | Mon 14:30-16:00, section 3                  |
| 2         | Mon 11:30-12:00, Thu 09:00-11:00, section 2 |
| 1         | Mon 09:30-11:00, section 1                  |
+-----------+---------------------------------------------+

 

Advertisements

Posted on: December 9, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.