This example illustrates how to append string of different column.
We use CONCAT() function to append the values of columns.
In this example, we are concatenating:
1. The value of "time" column,
2. String ", section" and
3. The
value of "sectionid" column
for each row of the "mca" table.
Query to create table "mca":
Query
CREATE TABLE mca (
studentid INT NOT NULL,
sectionid INT NOT NULL,
courseid INT NOT NULL PRIMARY KEY,
time VARCHAR(50)
);
Query to insert values in table:
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 to concatenate value to column values:
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 | +-----------+---------------------------------------------+
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.
Ask Questions? Discuss: MySQL Append String
Post your Comment