我需要在表中插入值,但在检查完条件后请帮助我
提前致谢
insert into b (SELECT userID,username, ANSWERTOQUESTION1 AS AnswerToQuestion From a
UNION ALL
SELECT userID,username, AnswerToQuestion2 AS aswertoquestion From a)
我需要在b表中插入值,如果不存在的话。 这意味着应该比较表中的所有值,以便我可以插入值
答案 0 :(得分:0)
我的unserstanding,你想把记录插入B表中而不是B中的A表,所以最后B表将包含A中的所有值。
如果确实如此,
我希望以下内容有所帮助
Insert into B
Select userid, username, answertoquestion1 from a
minus
Select userid, username, answertoquestion1 from b;
由于 Thangamani Eraniyan
答案 1 :(得分:0)
insert into b
select t2.userID,t2.username,concat(t2.AnswerToQuestion1,t2.AnswerToQuestion2)
from a as t2
LEFT JOIN b t3 on t3.userID=t2.userID;
要避免重复记录,请使用以下查询:
INSERT INTO b
SELECT t2.userID,t2.username,concat(t2.AnswerToQuestion1,t2.AnswerToQuestion2)
FROM a AS t2 WHERE NOT EXISTS (SELECT * FROM b AS t3 WHERE t2.userID = t3.userID);