miaoxu1com
V2EX  ›  数据库

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

  •  
  •   miaoxu1com · Dec 28, 2016 · 1562 views
    This topic created in 3429 days ago, the information mentioned may be changed or developed.

    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 replies    2016-12-28 17:17:25 +08:00
    kokdemo
        1
    kokdemo  
       Dec 28, 2016
    建议用一个临时变量先存一下 SELECT content FROM guideline_news WHERE id = 16

    然后再更新
    harborM
        2
    harborM  
       Dec 28, 2016
    mysql 不能直接 set select ,需要使用 inner join
    harborM
        3
    harborM  
       Dec 28, 2016
    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
    wesley
        5
    wesley  
       Dec 28, 2016
    update guideline_news a, guideline_news b set a.content=b.content
    where a.id>16 and b.id=16
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3958 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 96ms · UTC 05:17 · PVG 13:17 · LAX 22:17 · JFK 01:17
    ♥ Do have faith in what you're doing.