[英]Update a foreign key by matching a column other than primary key in another table
I am trying to update the FooID
column in Table_One
with a FooID
from table two. 我试图更新FooID
列Table_One
与FooID
从表中的两个。 I have the primary key for the rows I want to update for Table One
, but I need to get that ID from Table_Two
where a given Description
exists. 我具有要针对Table One
进行更新的行的主键,但是我需要从存在给定Description
Table_Two
中获取该ID。 So say I know that Table_One Primary_ID = 3
needs to have a FooID
of 3, because FooID
of Table_Two
has a description of "Bar." 可以这么说,我知道Table_One Primary_ID = 3
需要具有Table_One Primary_ID = 3
的FooID
,因为FooID
的Table_Two
具有对“ Bar”的描述。 What kind of SQL statement can I use to match the ID based on the value of the Description
? 我可以使用哪种SQL语句根据Description
的值来匹配ID?
Table_One 表_一
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| Primary_ID| int(11) | NO | PRI | NULL | |
| Statement | text | YES | | NULL | |
| FooID | int(11) | YES | MUL | NULL | |
+-----------+---------+------+-----+---------+-------+
Table_Two Table_Two
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| FooID | int(11) | NO | PRI | NULL | auto_increment |
| Description| varchar(140) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
So far I have tried a few things similar to: 到目前为止,我已经尝试了一些类似的方法:
UPDATE Table_one
INNER JOIN Table_Two ON (Table_One.FooID = Table_Two.FooID AND Table_Two.Description = 'Bar')
SET ksa.FooID = competency.FooID;
As well as: 以及:
UPDATE Table_One
SET
`FooID` = [SELECT FooID FROM Table_Two]
WHERE `Table_Two`.`Description` = 'Bar';
The first attempt returned zero results, and the second attempt kept saying the the column in Table_Two
does not exist. 第一次尝试返回零结果,第二次尝试一直说Table_Two
中的列不存在。 I tried a few other things as well but nothing has worked. 我也尝试了其他一些方法,但是没有任何效果。 These were the closest I came to something working. 这些是我最接近可以工作的东西。
If you want to update a known record from table one with a value that you want to get from a known record in table two, then you don't need anything too complex. 如果要使用表2中的已知记录获取的值更新表1中的已知记录,则不需要太复杂。 You could do this by fetching the value from table two in one query, and then updating the record in table one in a second. 您可以通过在一个查询中从表二中获取值,然后在一秒钟内更新表一中的记录来实现。 You can do this in one query though by using a subquery as a scalar operand . 尽管可以通过将子查询用作标量操作数来在一个查询中执行此操作 。
UPDATE Table_One SET FooID =
(SELECT FooID FROM Table_Two WHERE Description = 'Bar')
WHERE Primary_ID = 3;
The set in the first alternative seems to be wrong: 第一种选择中的集合似乎是错误的:
UPDATE Table_one
INNER JOIN Table_Two ON (Table_One.FooID = Table_Two.FooID AND Table_Two.Description = 'Bar')
SET ksa.FooID = competency.FooID;
Have you tried this: 您是否尝试过:
UPDATE Table_one
INNER JOIN Table_Two ON (Table_One.FooID = Table_Two.FooID AND Table_Two.Description = 'Bar')
SET FooID = Table_Two.FooID;
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.