简体   繁体   English

更新 MySQL 数据库表中具有不同值的多行

[英]Update multiple rows with different values in MySQL database table

I'm trying to select commenter from a table called comments then select username from a table called users using a where clauses from the details from the first table.我正在尝试 select 评论者来自名为 comments 的表,然后 select 用户名来自名为 users 的表,使用第一个表中详细信息中的 where 子句。 After that update comments set username to the usernames obtained from table 2.之后更新评论将用户名设置为从表 2 中获得的用户名。

This is my code:这是我的代码:

<?php 
include("connection.php") ;
$sql=mysqli_query($link, "SELECT * FROM comment ORDER BY id ASC" ) ;
while($row=mysqli_fetch_assoc($sql)){
    $commenter=$row['commenter'] ;

    $sqli=mysqli_query($link, "SELECT * FROM users WHERE fullname='$commenter' ORDER BY id ASC" ) ;
    while($rows=mysqli_fetch_assoc($sqli)){
        $username=$rows['username'] ;
        $sql_u=mysqli_query($link, "UPDATE comment SET username='$username' WHERE commenter='$commenter' " ) ;
    }
}
?>

The code above only updates one row上面的代码只更新一行

Here is a small correction of Barmar answer这是Barmar答案的一个小更正

UPDATE comment c
JOIN users u ON u.fullname = c.commenter
SET c.username = u.username

u.fullname = c.commenter instead of comment u.fullname = c.commenter 而不是评论

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

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