我正在尝试对我的数据库进行 SQL 查询。它必须在 table1
中搜索包含用户名的行,并将其 id 复制到 table2
中的列。
示例:在table1
中搜索用户“John”
并将John的id复制到table2
:
表1:
id | username | points
表2:
id | user_id
我实际上尝试过这个:
INSERT INTO table2(table2.user_id) SELECT table1.id WHERE table1.username = "John";
但它不起作用。我不知道该怎么做?你能帮我吗?
最佳答案
你能试试这个吗?
INSERT INTO table2 (user_id) SELECT t1.id FROM table1 AS t1 WHERE t1.username = 'John';
注意:如果 table1 上的列 id 是一个标识(运行号),这将起作用。
关于php - 将值从一个表复制到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46498744/