UPDATE with ORDER BY and LIMIT not working in MYSQL

前端 未结 5 555
囚心锁ツ
囚心锁ツ 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:04

    Read article about How to use ORDER BY and LIMIT on multi-table updates in MySQL

    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.

    0 讨论(0)
  • 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
    )
    
    0 讨论(0)
  • 2020-12-03 22:17

    Salam You can use this method and work properly !

    UPDATE Ratemaster, Rates 
    SET Ratemaster.Rate=Rates.Rate 
    WHERE Ratemaster.user=Rates.user 
    ORDER BY Rates.id
    LIMIT 1
    
    0 讨论(0)
  • 2020-12-03 22:18

    Work It 100%

    UPDATE table  SET Sing='p'  ORDER BY sr_no  LIMIT 10;  
    
    0 讨论(0)
  • 2020-12-03 22:25

    The problem is that LIMIT is only to be used with SELECT statements, as it limits the number of rows returned by the query.

    From: http://dev.mysql.com/doc/refman/5.5/en/select.html

    The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants, with these exceptions:

    Within prepared statements, LIMIT parameters can be specified using ? placeholder markers.
    
    Within stored programs, LIMIT parameters can be specified using integer-valued routine parameters or local variables as of MySQL 5.5.6.
    

    With two arguments, the first argument specifies the offset of the first row to return, and the second specifies the maximum number of rows to return. The offset of the initial row is 0 (not 1):

    SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15

    To retrieve all rows from a certain offset up to the end of the result set, you can use some large number for the second parameter. This statement retrieves all rows from the 96th row to the last:

    SELECT * FROM tbl LIMIT 95,18446744073709551615;

    With one argument, the value specifies the number of rows to return from the beginning of the result set:

    SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows

    In other words, LIMIT row_count is equivalent to LIMIT 0, row_count.

    For prepared statements, you can use placeholders. The following statements will return one row from the tbl table:

    SET @a=1; PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?'; EXECUTE STMT USING @a;

    The following statements will return the second to sixth row from the tbl table:

    SET @skip=1; SET @numrows=5; PREPARE STMT FROM 'SELECT * FROM tbl LIMIT ?, ?'; EXECUTE STMT USING @skip, @numrows;

    For compatibility with PostgreSQL, MySQL also supports the LIMIT row_count OFFSET offset syntax.

    If LIMIT occurs within a subquery and also is applied in the outer query, the outermost LIMIT takes precedence. For example, the following statement produces two rows, not one:

    (SELECT ... LIMIT 1) LIMIT 2;

    0 讨论(0)
提交回复
热议问题