我有 table
person
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
和
chatroom
+-----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| col1 | int(11) | YES | | NULL | |
| col2 | int(11) | YES | | NULL | |
+-----------+---------+------+-----+---------+----------------+
并且我想执行以下语句(对于每个 person
在 chatroom
中添加一行,分别包含 person.id
和
和 id
和 col2
列的 >DEFAULTNULL
值)
INSERT INTO chatroom (DEFAULT, col1, NULL) SELECT DEFAULT, id AS col1, NULL FROM person;
但它不起作用。 有人可以更正查询吗?
最佳答案
您的 id
字段是 auto_increment
,因此您可以将其从查询中删除,它会自动将行设置为下一个值。此外,由于 col2
的默认值为 NULL
,因此也可以将其排除在查询之外。它将自动设置为默认值。
INSERT INTO chatroom (col1) SELECT id FROM person
关于mysql - 使用 SELECT 和一些 DEFAULT 值插入 INTO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27660827/