是否可以在子查询中为 INSERT 选择列名?
例如,如果我有一个表 (table1),它将事件映射到另一个表 (table2) 中的列:
表格1:
+------+--------+---------+
| id | events | columns |
+------+--------+---------+
| 1 | event1 | column1 |
| 2 | event2 | column2 |
| 3 | event3 | column3 |
| 4 | event4 | column1 |
| 5 | event5 | column2 |
| 6 | event6 | column3 |
+------+--------+---------+
表2:
+------+---------+---------+---------+
| id | column1 | column2 | column3 |
+------+---------+---------+---------+
| ... | ... | ... | ... |
+------+---------+---------+---------+
是否有SQL语句,如:
INSERT INTO table2
(
id,
(SELECT columns /* the interesting subquery */
FROM table1
WHERE events='event1')
)
VALUES (1, 123);
这将导致插入 table2 的值:
+------+---------+---------+---------+
| id | column1 | column2 | column3 |
+------+---------+---------+---------+
| 1 | 123 | NULL | NULL |
+------+---------+---------+---------+