I am having an issue with MySQL. I want to have dynamic columns based on the rows. Here are the details
SELECT `marks`.`id` , `marks`.`studentID` , `marks`.`subjectID` , `marks`.`mark`
FROM `Mark` `marks`
LEFT OUTER JOIN `Student` `students` ON ( `students`.`id` = `marks`.`studentID` )
WHERE (
`students`.`classID` =1
)
LIMIT 0 , 30
My Output is
+----+-----------+-----------+------+
| id | studentID | subjectID | mark |
+----+-----------+-----------+------+
| 1 | 1 | 1 | 20 |
| 2 | 1 | 2 | 36 |
| 3 | 2 | 1 | 47 |
| 4 | 2 | 2 | 43 |
+----+-----------+-----------+------+
4 rows in set (0.00 sec)
Output I need is
+----+-----------+-----------+-----------+
| id | studentID | subject_1 | subject_2 |
+----+-----------+-----------+-----------+
| 1 | 1 | 20 | 36 |
| 2 | 2 | 47 | 43 |
+----+-----------+-----------+-----------+
4 rows in set (0.00 sec)
No of subjects can very depending on the enteries in the Subject table. I need only one row per user showing all the marks. Here are the table structure I use.
--
-- Table structure for table `Mark`
--
CREATE TABLE IF NOT EXISTS `Mark` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`studentID` int(11) NOT NULL,
`subjectID` int(11) NOT NULL,
`mark` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
--
-- Table structure for table `Student`
--
CREATE TABLE IF NOT EXISTS `Student` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`classID` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
--
-- Table structure for table `Subject`
--
CREATE TABLE IF NOT EXISTS `Subject` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB
Thanks is advance.