V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
miaoxu1com
V2EX  ›  数据库

查询表里边 id 为 16 的 content 然后,更新 id 大于 16 的 content 为查出来的 content

  •  
  •   miaoxu1com · 2016 年 12 月 28 日 · 1386 次点击
    这是一个创建于 3308 天前的主题,其中的信息可能已经有所发展或是发生改变。

    1.使用 sql 只查询一次 2.我自己的 sql: UPDATE guideline_news SET content = ( SELECT content FROM guideline_news WHERE id = 16 ) WHERE id > 16 备注: 这个 sql 报错:[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT content FROM guideline_news WHERE id=16 WHERE id>16' at line 1

    5 条回复    2016-12-28 17:17:25 +08:00
    kokdemo
        1
    kokdemo  
       2016 年 12 月 28 日
    建议用一个临时变量先存一下 SELECT content FROM guideline_news WHERE id = 16

    然后再更新
    harborM
        2
    harborM  
       2016 年 12 月 28 日
    mysql 不能直接 set select ,需要使用 inner join
    harborM
        3
    harborM  
       2016 年 12 月 28 日
    UPDATE guideline_news a inner join ( SELECT content FROM guideline_news WHERE id = 16 ) b SET a.content =b.content WHERE id > 16;试试这条语句;我在工作中也遇到过,百度就有解决方案: http://www.3lian.com/edu/2014/05-13/147149.html
    q397064399
        4
    q397064399  
       2016 年 12 月 28 日
    wesley
        5
    wesley  
       2016 年 12 月 28 日
    update guideline_news a, guideline_news b set a.content=b.content
    where a.id>16 and b.id=16
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2724 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:01 · PVG 20:01 · LAX 04:01 · JFK 07:01
    ♥ Do have faith in what you're doing.