Pakeeza

Pakeeza

  • 1.6k
  • 107
  • 4.9k

I like to set the priority on rows returned based on values

Nov 20 2023 6:16 PM

I have two tables one is students_Games and one is students tables both have a student_id a primary foriegn key relationship now based on their games I am putting them in some queue 

Student table {Student_id,name,Address}

Students_games(id,Student_ID(FOREIGNKEY,game_code),

 

table_ID student_id game Code
1 1 200
2 2 200
3 2 300
4 3 200
5 3 300
6 4 870

 

My Qyuery is like this 

Select * from studnets  ss

join students_games  gg on gg.Student_id=ss.student_Id where gg.Games_code in (200,300)

Now when i run this for student id 2 and 3 it will return two,two rows one for 200 and one for 300 , now I want to set the prioirty like if there is 200 available then it must return 200, if not then it should return 300 if both of them not present then it should return 0 . how to query it like that  i mean if 200 game code is available then chose it for every student and don't chose 300 for it if 200 is available if 200 is not available then chose 300 as game code if both of them not there then chose 0

 


Answers (5)