简体   繁体   English

mysql更新表与另一个表的值?

[英]mysql update table with values from another table?

I am trying to update my table 'supplier_stats' with the values from my other table 'supplier_change_request'. 我正在尝试使用我的其他表'supplier_change_request'中的值更新我的表'supplier_stats'。

My two tables look like the following: 我的两个表格如下所示:

Supplier_change_request Supplier_change_request

id   |   user_id   |   company_name   |   supplier_number
1        123           hewden             V0001

Supplier_stats Supplier_stats

Id | user_id | company_name  |  address  |   reference   | supplier_number
1    123       pie              n/a          12345         V0001
2    145       gates            n/a          12345         V0002

Here is my MySQL: 这是我的MySQL:

$reference = '12345' $ reference ='12345'

$query = "UPDATE supplier_stats 
SET supplier_stats.company_name = (
    SELECT supplier_change_request.company_name 
    FROM supplier_change_request
    WHERE supplier_change_request.reference = '$reference' AND supplier_change_request.supplier_number = supplier_stats.supplier_number";
mysql_select_db('hewden1');
$retval = mysql_query( $query, $conn )

by my calculation this should be setting the value of company_name where supplier_number is 'V0001' in my table 'supplier_stats' to 'hewden'. 通过我的计算,这应该设置company_name的值,其中supplier_number在我的表'supplier_stats'中为'V0001'为'hewden'。 However the company_name is not being updated. 但是,company_name未更新。

Can someone please show me where I am going wrong? 有人可以告诉我哪里出错了吗? Thank you in advance 先感谢您

I think the syntax is a bit off in your query and that it should look like this (just the SQL, adapt to PHP as needed): 我认为你的查询中的语法有点偏,它应该看起来像这样(只是SQL,根据需要适应PHP):

UPDATE supplier_stats ss
JOIN supplier_change_request scr ON scr.supplier_number = ss.supplier_number
SET ss.company_name = scr.company_name 
WHERE ss.reference = '$reference' 

The column reference pointed to the supplier_change_request in your sample query, but to supplier_stats in your sample data - I assumed the sample data was correct; reference指向示例查询中的supplier_change_request ,但指向示例数据中的supplier_stats - 我假设示例数据是正确的; change if not. 如果没有改变。

This query should change the company_name in supplier_stats from pie to hewden . 此查询应将supplier_statscompany_namepie更改为hewden

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM