zoukankan      html  css  js  c++  java
  • mysql update 如何写子查询

    当使用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

  • 相关阅读:
    A. Dreamoon and Stairs(Codeforces Round #272)
    bootstrap之UpdateStrings
    FZU
    IT忍者神龟之 oracle行转列、列转行
    linux find 10天内改动过的文件
    内核调试日志打印宏
    ack-grep 代码全文搜索
    JDK配置 linux
    IDA修改游戏
    curl 访问https问题
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/13633423.html
Copyright © 2011-2022 走看看