我有一个列名为id的用户表,father_email,mother_email,email_notification等等,现在我想用
更新email_notification列father_email,mother_email 我是通过多个查询完成的,但我想知道如何只使用单个查询来执行此操作,以便节省执行时间。
我的代码如下:
<?php
$qry="SELECT id,CONCAT(father_email,',',mother_email) as notify FROM user";
$query=mysql_query($qry);
while($row=mysql_fetch_assoc($query))
{
$qry2="UPDATE user SET email_notification='".$row['notify']."' WHERE id=".$row['id']."";
mysql_query($qry2);
}
?>
它工作正常,但我想知道如何使用单个查询
答案 0 :(得分:3)
这将更新所有用户的所有email_notification列:
UPDATE user
SET email_notification = CONCAT_WS(',', father_email, mother_email)
(我认为最好使用在parent_email或mother_email中跳过空值的CONCAT_WS)
答案 1 :(得分:0)
这将取代所有这些
UPDATE user AS u
INNER JOIN user AS ul ON ul.id = u.id
SET u.email_notification = CONCAT(ul.father_email,',',ul.mother_email)
UPDATE user
SET email_notification = CONCAT_WS(',', father_email, mother_email)