Jump to content

[SOLVED] SQL JOIN issue


arifsor

Recommended Posts

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
Share on other sites

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 left outer join 
  qualification level_one on u.id = level_one.user_id left outer join
  qualification level_two on u.id = level_two.user_id left outer 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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.