[英]Mysql Insert with the Select
I have two tables named employees
and testdept
.In employees
table there are two fields as employee_id
and department
. 我有两个名为employees
和testdept
表。在employees
表中有两个字段employee_id
和department
。 testdept
table contains two fields as epfno
and deptid
. testdept
表包含两个字段,如epfno
和deptid
。
Now what I want to do is insert those id's from testdept
table to employees
table department
field where employee_id
matches with the epfno. 现在,我要做的是将那些ID从testdept
表插入到employees
表department
字段中, employee_id
与epfno匹配。
I have tried with the below code. 我试过下面的代码。 It inserts the data as a new row. 它将数据插入为新行。 That is the problem. 那就是问题所在。
INSERT INTO employees (department)
SELECT t.deptid
FROM testdept t, employees e
WHERE t.epfno = e.employee_id
If I understood you correctly, you want an UPDATE
statement and not an INSERT
: 如果我对您的理解正确,那么您需要UPDATE
语句而不是INSERT
:
UPDATE employees e
JOIN testdept t
ON(e.employee_id = t.epfno)
SET e.department = t.deptid
This will update the records in employee
table with the data from the corresponding row in testdept
table 这将使用testdept
表中相应行的数据更新employee
表中的记录
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.