Concatenating String Values: Joining Strings
/*
mysql> SELECT ClassID, CONCAT(Time, ', Room ', RoomID) AS ClassDetails
-> FROM Class;
+---------+------------------------------------------+
| ClassID | ClassDetails |
+---------+------------------------------------------+
| 1 | Mon 09:30-11:00, Room 6 |
| 2 | Mon 11:30-12:00, Thu 09:00-11:00, Room 5 |
| 3 | Mon 14:30-16:00, Room 3 |
+---------+------------------------------------------+
3 rows in set (0.00 sec)
*/
/* Prepare the data */
Drop TABLE Class;
CREATE TABLE Class (
CourseID INT NOT NULL,
RoomID INT NOT NULL,
ClassID INT NOT NULL PRIMARY KEY,
Time VARCHAR(50)
)TYPE = InnoDB;
/* Insert data for testing */
INSERT INTO Class (ClassID,CourseID,RoomID,Time) VALUES (1,1,6,'Mon 09:30-11:00');
INSERT INTO Class (ClassID,CourseID,RoomID,Time) VALUES (2,2,5,'Mon 11:30-12:00, Thu 09:00-11:00');
INSERT INTO Class (ClassID,CourseID,RoomID,Time) VALUES (3,3,3,'Mon 14:30-16:00');
/* Real command */
SELECT ClassID, CONCAT(Time, ', Room ', RoomID) AS ClassDetails
FROM Class;
Related examples in the same category