dongxing2710 2013-04-17 07:44
浏览 52
已采纳

如何使用单个查询更新具有相同记录的字段的记录?

I have one user table with column name id,father_email,mother_email,email_notification and many more, now i want to update email_notification column with

father_email,mother_email I did it with multiple queries but i want to know that how to do it with single query only so it saves the execution time.

my code are as follows:

<?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);
    }
?>

its working fine but i want to know how to do it with single query

  • 写回答

2条回答 默认 最新

  • dongqiangou5724 2013-04-17 07:45
    关注

    This will update all email_notification columns for all users:

    UPDATE user
    SET email_notification = CONCAT_WS(',', father_email, mother_email)
    

    (i think it's better to use CONCAT_WS that will skip null values in father_email or in mother_email)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献