我有一个具有这种结构的表:
+----+-------------------+
| id | identical_with_id |
+----+-------------------+
id
是自动递增的,我需要在此列identical_with_id
中插入与id
的值完全相同的值。我怎么能这样做?
我想要这样的事情:
+----+-------------------+
| id | identical_with_id |
+----+-------------------+
| 1 | 1 |
| 2 | 2 |
| 3 | 3 |
+----+-------------------+
注意,实际上该表还包含identical_with_id
列的其他一些值,如下所示:
+----+-------------------+
| id | identical_with_id |
+----+-------------------+
| 1 | 1 | -- it is a question
| 2 | 1 | -- it is the answer of the above question
| 3 | 1 | -- it is the answer of the above question
| 4 | 4 | -- it is a question
| 5 | 4 | -- it is the answer of the above question
| 6 | 6 | -- it is a question
| 7 | 7 | -- it is a question
| 8 | 7 | -- it is the answer of the above question
| 9 | 7 | -- it is the answer of the above question
+----+-------------------+
现在我需要在表格中插入一个问题。我想知道如何填写此专栏:identical_with_id
?
答案 0 :(得分:1)
无需更新表格。您可以在单个插入查询中执行此操作,如下所示。
INSERT INTO `table_name` VALUES(NULL, (SELECT `AUTO_INCREMENT` FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'database_name' AND TABLE_NAME = 'table_name'));
虽然我没有注意到这一点......