• mysql 在update中实现子查询的方式


    本文转自:91博客;原文地址:http://www.9191boke.com/644523640.html

    当使用mysql条件更新时--最先让人想到的写法  

    UPDATE buyer SET is_seller=1 WHERE uid IN (SELECT uid FROM seller) 

    此语句是错误的,会报错 You can't specify target table 'xxx' for update in FROM

    这是因为:

    mysql的update的一些特点

    1、update 时,更新的表不能在set和where中用于子查询;

    2、update 时,可以对多个表进行更新(sqlserver不行);

             如:update ta a,tb b set a.Bid=b.id ,b.Aid=a.id;  

    3、update 后面可以做任意的查询,这个作用等同于from;

    正确的方式是,例:

    简单的更新:

    UPDATE `roles_permissions` a SET a.roles_id=89 WHERE a.`roles_name`='0|||管理员'

    较为复杂的更新:

    例1:

    UPDATE order_mall a,(SELECT order_mall.id FROM `order_mall`,`order_goods` WHERE order_mall.`id`=order_goods.`order_id` AND order_goods.order_status=8 AND order_goods.order_goods_type=3) b
    SET a.`status`=4
    WHERE a.id=b.id

    例2:

    UPDATE `core_user` a,(SELECT message_push_conf.user_id,message_push_conf.`open_push` FROM `message_push_conf`) b
    SET a.`switch_push`=b.open_push
    WHERE a.id=b.user_id

  • 相关阅读:
    Nginx 跨域配置支持
    数据结构与算法分析
    数据结构与算法分析
    数据结构与算法分析
    Bash shell
    Bash shell
    HHUOJ 1040
    HHUOJ 1114
    HDUOJ 1171
    HDUOJ 1428
  • 原文地址:https://www.cnblogs.com/007sx/p/7404651.html
Copyright © 2020-2023  润新知