INSERT INTO TABLE WEEKS (ID, WK1) SELECT ID, WEEK1 FROM TBL1 WHERE ID = 100;
INSERT INTO TABLE WEEKS (ID, WK2) SELECT ID, WEEK2 FROM TBL1 WHERE ID = 100;
INSERT INTO TABLE WEEKS (ID, WK3) SELECT ID, WEEK3 FROM TBL1 WHERE ID = 100;
INSERT INTO TABLE WEEKS (ID, WK4) SELECT ID, WEEK4 FROM TBL1 WHERE ID = 100;
有没有办法将这些陈述合并为一个?
答案 0 :(得分:1)
INSERT INTO TABLE WEEKS (ID, WK1, WK2, WK3, WK4)
SELECT ID, WEEK1, NULL, NULL, NULL FROM TBL1 WHERE ID = 100;
UNION SELECT ID, NULL, WEEK2, NULL, NULL FROM TBL1 WHERE ID = 100;
UNION SELECT ID, NULL, NULL, WEEK3, NULL FROM TBL1 WHERE ID = 100;
UNION SELECT ID, NULL, NULL, NULL, WEEK4 FROM TBL1 WHERE ID = 100;
像其他人说的那样,这似乎不是一个好的桌面设计。如果可以,您应该考虑规范化表结构。
答案 1 :(得分:0)
编辑原始问题的更新方式:
你可以使用@ adam0101所述的UNIONS组合这些INSERT语句,但它不会比使用4个INSERT语句更快。
请注意,您确实应该将数据库重新设计为normalized 。这样做可以减少重复数据并提高数据库的性能。
答案 2 :(得分:0)
如果您只想合并查询,可以使用以下内容:
INSERT INTO TABLE WEEKS (ID, WK1,WK2,WK3,WK4) SELECT ID, 1,2,3,4 FROM TBL1
WHERE ID = 100;