how to select table based on id even/odd in another table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • minlilac
    New Member
    • Dec 2018
    • 1

    how to select table based on id even/odd in another table

    Hi, I want to ask. i have 2 quiz tables that consists of quizzes and also student table. i want to select the quiz table based on when the student register id even, the student will get quiz table set 1, and if student id is odd, get the second quiz table.
    i have set the id as primary key in student table
    can anyone help mee?

    im using php language and mysql.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    Use the MOD function

    Code:
    SELECT MOD(1,2), MOD(2,2), MOD(3,)
    This returns: 1,0,1

    Comment

    • AjayGohil
      New Member
      • Apr 2019
      • 83

      #3
      Hi,

      you can use store procedure and pass student Id as parameter.

      Code:
      DELIMITER $$
       
      CREATE PROCEDURE GetCustomerLevel(IN  studentId Int)
      BEGIN
       
          IF ((studentId%2)==0) THEN
              select *from quiz_table1;
          ELSE
              select *from quiz_table2;
          END IF;
      
      END$$
       
      DELIMITER ;
      or you can use select If() statement in following way:

      Code:
      SELECT IF((studentId%2)==0),select *from quiz_table1,   select *from quiz_table2) from student;

      you can refer following site for creating store procedure in mysql :


      Comment

      Working...