简体   繁体   English

Mysql查询在多个条件和列上从一个表插入到另一个表

[英]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 ). 我有一个名为作为其中我共有8列的本身(测试表表Roll_noStudent_nameCompany1Pass_fail1Company2Pass_fail2Company3Pass_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 ); 访谈表的列是( Roll_noStudent_nameCompany1Company2Company3 );

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. 如果学生通过了pass_fail1=1的考试(即pass_fail1=1 ),则他有资格接受pass_fail1=1的面试,其余两个公司的条件相同。 So, Company1 will be inserted in interview table only if pass_fail1=1. 因此,仅当pass_fail1=1.时, pass_fail1=1.才会插入到采访表中pass_fail1=1.

if pass_fail=0 then do not enter the name of the company. 如果pass_fail=0则不要输入公司名称。

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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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