[英]Mysql INSERT data in Table 2 for each row in Table 1
I have two tables. 我有两张桌子。
Table A is a list of questions that needs to be filtered by question type. 表A是需要按问题类型过滤的问题列表。 eg SELECT * from TableA WHERE Qtype = "whatever" 例如SELECT * from TableA WHERE Qtype =“ whatever”
I then need to add four new records to Table B for each record pulled by the query on TableA. 然后,我需要为TableA上的查询提取的每个记录向表B添加四个新记录。
Table A and Table B join on an ID number - TableA.id = TableB.questionid 表A和表B连接在一起的ID号-TableA.id = TableB.questionid
I am presuming I will need to run it four times, once for each record that I add. 我想我将需要运行四次,每次添加的记录一次。
I am getting confused on the FOR EACH command in conjunction with an INNER JOIN 我对FOR EACH命令和INNER JOIN感到困惑
Would appreciate any help! 将不胜感激!
I'm afraid I still don't fully understand your question, but this will insert three records into TableB
for each record of TableA
, with three different values for newfield
: 恐怕我仍然不能完全理解您的问题,但这将为TableA
每个记录将三个记录插入TableB
,其中newfield
三个不同值:
INSERT INTO TableB (ID, newfield) (
SELECT a.ID, n.newfield
FROM (
SELECT 'Excellent' AS newfield UNION ALL
SELECT 'Something' UNION ALL
SELECT 'Something else'
) n
CROSS JOIN TableA a
)
Try this 4 times : 尝试4次:
INSERT INTO TableB (questionid, otherfieldB)
VALUES (
SELECT id, otherfieldA
FROM TableA
WHERE Qtype = "whatever"
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.