[英]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_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
); 访谈表的列是(
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. 如果学生通过了
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.