1

I have 2 MYSQL tables and when I pick up dates from table A which contains only 1 column and lets say 10 rows with dates

    $result = mysql_query("SELECT * FROM A");
    $row = mysql_fetch_assoc($result);

And after I want to UPDATE another table B with using this dates from table A mysql_query("UPDATE B SET something='1' WHERE name='".$row['name']."'") So i need to update the second table, but its updating just once with first date from table A, and other 9 its ignoring. So my question is, how to make updating of second table with each date from 1 table?

4

1 回答 1

0

您需要循环运行更新。执行查询后

$result = mysql_query("SELECT * FROM A");

并验证它是否成功(确保$result不为空),而不是获取一行,而是使用循环:

while($row = mysql_fetch_assoc($result)){
    // perform calculations & assignments with the $row elements
    $result2 = mysql_query("UPDATE B SET something='1'
                            WHERE name='".$row['name']."'");
    if(! $result2){
         echo "update failed";
    }
    // Any other stuff you need to do
}

或者:

如果something更新中的所有行都相同,则可以更改第一个查询以提供以逗号分隔的名称字符串:

$result = mysql_query("SELECT CONCAT("'",GROUP_CONCAT(name SEPARATOR "','"),"'")
                       AS all_names FROM A");

这样,您只会收到一行,然后您可以在第二个查询中使用它:

$result2 = mysql_query("UPDATE B SET something='1'
                        WHERE name IN (".$row['name'].")");
于 2013-04-22T19:31:47.360 回答