Php 如何从2个表中选择和更新
我使用的是PHP和MySQL,我有两个表,其中包含Php 如何从2个表中选择和更新,php,mysql,sql,Php,Mysql,Sql,我使用的是PHP和MySQL,我有两个表,其中包含number和name字段。默认情况下,表1中的name字段为空,如果表2中有一行具有匹配的编号,我希望更新表1中的名称 以下伪代码说明了这一点: select number, number from table1, table2 if number from table1 == number from table2 then insert or update name from table1 with the value of na
number
和name
字段。默认情况下,表1中的name
字段为空,如果表2中有一行具有匹配的编号,我希望更新表1中的名称
以下伪代码说明了这一点:
select number, number
from table1, table2
if number from table1 == number from table2
then insert or update name from table1 with the value of name from table2
在MySQL中,您可以这样做
UPDATE table1 t1
INNER JOIN table2 t2 ON t1.number = t2.number
SET t1.name = t2.name
如果您不能依赖于加入update语句,那么更符合ANSI标准的答案将依赖于子查询,例如:
UPDATE table1 t1
SET name = (SELECT name FROM table2 t2 WHERE t2.number = t1.number)
WHERE EXISTS (SELECT 1 FROM table2 t2 WHERE t2.number = t1.number)
WHERE确保您只更新有匹配行的列。您的问题不清楚!!这个问题不够清楚,我无法给你一个确切的答案。但我想你正在寻找请访问这个我更清楚的问题连同表结构和图像我相信你可以帮助我更多。请看这个。提前谢谢