UPDATE with ORDER BY and LIMIT not working in MYSQL

前端 未结 5 562
囚心锁ツ
囚心锁ツ 2020-12-03 21:32

I am new to MYSQL, and unable to resolve or even with so many answers on this forum, unable to identiy the error in this statement. I am using MYSQL database.

I have

5条回答
  •  庸人自扰
    2020-12-03 22:13

    Usually you can use LIMIT and ORDER in your UPDATE statements, but in your case not, as written in the MySQL Documentation 12.2.10. UPDATE Syntax:

    For the multiple-table syntax, UPDATE updates rows in each table named in table_references that satisfy the conditions. In this case, ORDER BY and LIMIT cannot be used.

    Try the following:

    UPDATE Ratemaster
    SET Ratemaster.Rate =
    (
        SELECT Rates.Rate
        FROM Rates
        WHERE Ratemaster.user = Rates.user
        ORDER BY Rates.id
        LIMIT 1
    )
    

提交回复
热议问题