469,342 Members | 6,008 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,342 developers. It's quick & easy.

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.
Dec 10 '18 #1
2 3617
Luuk
1,043 Expert 1GB
Use the MOD function

Expand|Select|Wrap|Line Numbers
  1. SELECT MOD(1,2), MOD(2,2), MOD(3,)
This returns: 1,0,1
Dec 14 '18 #2
AjayGohil
84 64KB
Hi,

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

Expand|Select|Wrap|Line Numbers
  1. DELIMITER $$
  2.  
  3. CREATE PROCEDURE GetCustomerLevel(IN  studentId Int)
  4. BEGIN
  5.  
  6.     IF ((studentId%2)==0) THEN
  7.         select *from quiz_table1;
  8.     ELSE
  9.         select *from quiz_table2;
  10.     END IF;
  11.  
  12. END$$
  13.  
  14. DELIMITER ;
  15.  
  16.  
or you can use select If() statement in following way:

Expand|Select|Wrap|Line Numbers
  1. 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 :


https://dev.mysql.com/doc/refman/8.0...procedure.html
Mar 4 '20 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Haas C | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.