arifsor Posted July 24, 2008 Share Posted July 24, 2008 please help me related to the db sch. user id | user_name ---|---------------- 1 | Willium 2 | John -------------------- qualification id | user_id | level | marks_obtained --------------------------------------------- 1 | 1 | 1 | 50 --------------------------------------------- 2 | 1 | 2 | 80 ---------------------------------------------- 3 | 1 | 3 | 90 --------------------------------------------- 4 | 2 | 1 | 50 --------------------------------------------- 5 | 2 | 2 | 40 --------------------------------------------- Required out put report user_name | level_one_marks | level_two_marks | level_three_marks ------------------------------------------------------------------------------------------- Willium | 50 | 80 | 90 John | 50 | 40 | NULL -------------------------------------------------------------------------------------------- * my query select u.user_name, level_one.marks_obtained as level_one_marks, level_two.marks_obtained as level_two_marks , level_three.marks_obtained as level_three marks from user u inner join qualification level_one on u.id = level_one.user_id inner join qualification level_two on u.id = level_two.user_id inner join qualification level_three on u.id = level_three.user_id where level_one.level = 1 and level_tow.level = 2 and level_three.level = 3 but above query only fetch one row user_name | level_one_marks | level_two_marks | level_three_marks ------------------------------------------------------------------------------------------- Willium | 50 | 80 | 90 -------------------------------------------------------------------------------------------- becauze level three mark is not availbel in qualification table for the user id 2 how can i produce my required report? please help Link to comment https://forums.phpfreaks.com/topic/116457-solved-query-join-issue/ Share on other sites More sharing options...
revraz Posted July 24, 2008 Share Posted July 24, 2008 I don't think you should be using the level as a join, you should just display it Link to comment https://forums.phpfreaks.com/topic/116457-solved-query-join-issue/#findComment-598891 Share on other sites More sharing options...
Barand Posted July 24, 2008 Share Posted July 24, 2008 try LEFT JOIN instead of INNER JOIN Link to comment https://forums.phpfreaks.com/topic/116457-solved-query-join-issue/#findComment-598913 Share on other sites More sharing options...
arifsor Posted July 25, 2008 Author Share Posted July 25, 2008 left join is also not working? any idea. Link to comment https://forums.phpfreaks.com/topic/116457-solved-query-join-issue/#findComment-599214 Share on other sites More sharing options...
Barand Posted July 25, 2008 Share Posted July 25, 2008 try $q = "SELECT u.user_name, SUM(IF(q.level=1, marks_obtained, 0)) AS level_one_marks, SUM(IF(q.level=2, marks_obtained, 0)) AS level_two_marks , SUM(IF(q.level=3, marks_obtained, 0)) AS level_three marks FROM user u INNER JOIN qualification q ON u.id = q.user_id "; Link to comment https://forums.phpfreaks.com/topic/116457-solved-query-join-issue/#findComment-599244 Share on other sites More sharing options...
arifsor Posted July 25, 2008 Author Share Posted July 25, 2008 Thanks Link to comment https://forums.phpfreaks.com/topic/116457-solved-query-join-issue/#findComment-599247 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.