MySQL INSERT多行具有相同的WHERE子句

时间:2016-03-29 18:37:48

标签: mysql

我目前能够使用以下SQL将行插入表中:

INSERT INTO ots (score_id,tab_id) VALUES ((SELECT id FROM score WHERE score_id = 11),(SELECT tab_id FROM tab WHERE layover_state = 7))
INSERT INTO ots (score_id,tab_id) VALUES ((SELECT id FROM score WHERE score_id = 12),(SELECT tab_id FROM tab WHERE layover_state = 7))
INSERT INTO ots (score_id,tab_id) VALUES ((SELECT id FROM score WHERE score_id = 19),(SELECT tab_id FROM tab WHERE layover_state = 9))
INSERT INTO ots (score_id,tab_id) VALUES ((SELECT id FROM score WHERE score_id = 27),(SELECT tab_id FROM tab WHERE layover_state = 10))
INSERT INTO ots (score_id,tab_id) VALUES ((SELECT id FROM score WHERE score_id = 45),(SELECT tab_id FROM tab WHERE layover_state = 10))
INSERT INTO ots (score_id,tab_id) VALUES ((SELECT id FROM score WHERE score_id = 51),(SELECT tab_id FROM tab WHERE layover_state = 10))
INSERT INTO ots (score_id,tab_id) VALUES ((SELECT id FROM score WHERE score_id = 52),(SELECT tab_id FROM tab WHERE layover_state = 11))

上述INSERT INTO语句可以正常使用。但是,它们看起来非常低效,因为应该有一些方法可以使用列表来捕获我需要指定的所有整数作为嵌套SELECT语句的一部分。但是,我不确定如何将score_id放入SQL列表,同时在单独的列表中保留layover_state的顺序。

问题

有没有办法用一行使用2个SQL列表替换上面的代码行?

其他信息:

我在MySQL Workbench中使用MySQL。

0 个答案:

没有答案