MYSQL sum() for distinct rows with left join


Ashish Saxena

i have three table:

DROP TABLE IF EXISTS `college_details`;

CREATE TABLE `college_details` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`student_id` int(10) DEFAULT NULL,
`collegename` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;

/*Data for the table `college_details` */

insert into `college_details`(`id`,`student_id`,`collegename`) values (1,1,'MMMEC,GKP'),(2,1,'MMMTU,GKP'),(3,2,'LPUT,JAL'),(4,3,'LINGAYS');

/*Table structure for table `course_details` */

DROP TABLE IF EXISTS `course_details`;

CREATE TABLE `course_details` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`student_id` int(10) DEFAULT NULL,
`fee` int(10) DEFAULT NULL,
`coursename` varchar(400) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

/*Data for the table `course_details` */

insert into `course_details`(`id`,`student_id`,`fee`,`coursename`) values (1,1,25,'Linux'),(2,2,25,'php'),(3,3,205,'php6');

/*Table structure for table `student_details` */

DROP TABLE IF EXISTS `student_details`;

CREATE TABLE `student_details` (
`ID` int(10) NOT NULL AUTO_INCREMENT,
`NAME` varchar(50) DEFAULT NULL,
`SEX` varchar(1) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;

/*Data for the table `student_details` */

insert into `student_details`(`ID`,`NAME`,`SEX`) values (1,'Ashish','m'),(2,'harpreet','m'),(3,'tanuj','m');

Relation among them :

Student  —– >…

View original post 246 more words