【问题标题】:Insert into a table from other table with condition从有条件的其他表中插入到表中
【发布时间】:2018-05-08 13:01:01
【问题描述】:
我有两张表,分别是表国家和公司。我想将 country_id 从表 country 复制到表 company 的 country_id 列。
Table
这是我当前的 mysql 查询:
INSERT INTO company (country_id) SELECT a.country_id FROM country a, company b WHERE a.country_name = b.country_name
但它不起作用。结果是创建新行而不是填写公司行
【问题讨论】:
标签:
mysql
mysql-workbench
【解决方案1】:
听起来你真的想在此处进行更新:
UPDATE company a
INNER JOIN country b
ON a.country_name = b.country_name
SET country_id = b.country_id;
【解决方案2】:
这听起来像是您想要进行更新,而不是插入。
UPDATE company AS cmp
LEFT JOIN country AS cnt
ON cmp.country_name=cnt.country_name
SET cmp.country_id=cnt.country_id;