简体   繁体   中英

Mysql Query for inserting from one table to another on Multiple conditions and columns

I have a table named as test table in which I have total of 8 columns as such ( Roll_no , Student_name , Company1 , Pass_fail1 , Company2 , Pass_fail2 , Company3 , Pass_fail3 ). I have to insert the data from test table to another table (say Interview table). Columns of Interview table are ( Roll_no , Student_name , Company1 , Company2 , Company3 );

The condition for insertion is:
If a student has passed the test of Company1 (ie pass_fail1=1 ) then he is eligible for interview of Company1,and same conditions for remaining two companies. So, Company1 will be inserted in interview table only if pass_fail1=1.

if pass_fail=0 then do not enter the name of the company.

Try this:

INSERT INTO interviews (Rollno,Student_name,Company1,Company2,Company3)
SELECT Rollno, Student_name, 
      (CASE WHEN Pass_fail1 = 1 THEN Company1 ELSE '' END) Company1, 
      (CASE WHEN Pass_fail2 = 1 THEN Company2 ELSE '' END) Company2, 
      (CASE WHEN Pass_fail3 = 1 THEN Company3 ELSE '' END) Company3 
FROM test 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM