简体   繁体   English

通过匹配另一个表中主键以外的列来更新外键

[英]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. 我试图更新FooIDTable_OneFooID从表中的两个。 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 = 3FooID ,因为FooIDTable_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.

相关问题 使用另一个表的主键更新外键列 - Update foreign key column with another table's primary key 更新一个表中的主键,这是另一表中的外键 - Update primary key in one table which is foreign key in another table 更改主键列,该主键列也是另一个表的外键-MySQL - Alter primary key column that is also a foreign key of another table - Mysql 连接到主键以外的其他表 - Joining to another table on fields other than its primary key 将内部联接表与主键以外的列链接 - Linking an inner join table with a column other than the primary key 如何从两个表中查询匹配表中的外键和另一个表中的主键 - How do I query from two tables matching the foreign key from table with the primary key from another mysql:更新一个表的主键,该主键被另一个表称为外键 - mysql: update a table's primary key which is referred by another table as foreign key MySQL外键使用多个字段来引用另一张表中的主键 - MySQL foreign key using more than one field to reference to a primary key from another table 如何在MySQL中更改表中的主键和表中的外键的列? - How to alter a column which is a primary key in the table and a foreign key in another table in MySQL? 如何在其他相关表中更新PRIMARY KEY / FOREIGN KEY? - How to update the PRIMARY KEY/FOREIGN KEY in other related tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM