简体   繁体   English

MySQL-如何从具有多个WHERE条件的另一个表更新表

[英]Mysql - how to UPDATE table from another table with several WHERE conditions

I have 2 tables: 我有2张桌子:

table1 which looks like: table1看起来像:

column1 | 第1列| column2 | 第2列| item1 | item1 | item2 | item2 | item3 | item3 | item4 | item4 | (it goes till item50) (直到项目50)

value | 价值| value | 价值| value | 价值| value | 价值| value | 价值| value | 价值| ......... .........

table_to_update that looks like: table_to_update看起来像:

column1 | 第1列| colum2 | colum2 | item 项目

value | 价值| value | 价值| value of item1 item1的值

value | 价值| value | 价值| value of item2 item2的值

value | 价值| value | 价值| value of item3 item3的值

value | 价值| value | 价值| value of item4 item4的值

value | 价值| value | 价值| value of item.... 物品的价值...

when I change in table1 the value of colum1 or column2 then I want these data changed in table_to_update. 当我在table1中更改colum1或column2的值时,我希望这些数据在table_to_update中更改。 Note values of items 1-50 never change 注释项1-50的值永远不变

So I can update value of item1 by using this PHP commands: 因此,我可以使用以下PHP命令更新item1的值:

UPDATE table_to_update, table1
SET table_to_update.column1 = table1.column1, table_to_update.column2 = table1.column2
WHERE table_to_update.item = table1.item1

Then value of item2 with: 然后item2的值包括:

UPDATE table_to_update, table1
SET table_to_update.column1 = table1.column1, table_to_update.column2 = table1.column2
WHERE table_to_update.item = table1.item2

(all the same but table1.item1 is changed to table1.item2) (所有相同,但table1.item1更改为table1.item2)

And do that 50 times which is obviously not very convenient 并做50次,这显然不是很方便

Is there a way to only have the WHERE part changed, such as something like this (which is wrong): 有没有办法只更改WHERE部分,例如这样(这是错误的):

UPDATE table_to_update, table1
SET table_to_update.column1 = table1.column1, table_to_update.column2 = table1.column2
WHERE table_to_update.item = table1.item1 ("THEN") table_to_update.item = table1.item2 ("THEN") table_to_update.item = table1.item3 ....

Alternatively I can write commands like below for the 50 items: 另外,我可以为50个项目编写如下命令:

UPDATE table_to_update, table1
SET table_to_update.column1 = table1.column1, table_to_update.column2 = table1.column2
WHERE table_to_update.item = table1.item1

UPDATE table_to_update, table1
SET table_to_update.column1 = table1.column1, table_to_update.column2 = table1.column2
WHERE table_to_update.item = table1.item2

but I don't know how to have them all run from 1 PHP page. 但是我不知道如何从1个PHP页面运行它们。 It looks like I need to create 50 PHP pages with 1 statement each? 看来我需要创建50个PHP页面,每个页面包含1条语句?

$data = mysql_fetch_assoc(mysql_query('SELECT * FROM table1'));

foreach ($data as $row) {

    mysql_query("UPDATE table_to_update SET column1 = '{$row['column1']}', column2 = '{$row['columnd2']}' WHERE item = '{$row['item1']}'");



}

I think thats where you need to be heading. 我认为这就是您需要前往的地方。 But it needs more work and is not secure. 但是它需要更多的工作并且并不安全。 If anyone wants to contribute go ahead. 如果有人想贡献,那就继续吧。

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

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