16

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 2 tables: Ratemaster and rates, in which a customer can have 1 product with different rates. Because of this, there is a duplication of customer and product fields, only the rate field changes. Now Table Ratemaster has all the fields : id, Customer code, Product, Rate, user whereas Table Rates has only: id, cust code, Rate, user. - user field is for checking session_user.

Now Table Ratemaster has 3 records with all field values being same except Rate field empty. Table Rates has different rates. I want to have all rates to be updated in Ratemaster from Rates table. I am unable to do this with UPDATE and LIMIT mysql command, it is giving error as:

Incorrect usage of UPDATE and LIMIT

UPDATE Ratemaster, Rates 
SET Ratemaster.Rate=Rates.Rate 
WHERE Ratemaster.user=Rates.user 
LIMIT 1
John Woo
  • 258,903
  • 69
  • 498
  • 492
user1114409
  • 565
  • 3
  • 13
  • 26
  • 3
    Where is your `ORDER BY` ??? (your question says with ORDER BY) – Manse Jan 31 '12 at 13:38
  • Hi, I have tried with ORDER BY also, it gives the same error: Incorrect usage of UPDATE and ORDER BY. – user1114409 Jan 31 '12 at 13:43
  • Then show us that query - LIMIT is meaningless ORDER BY – symcbean Jan 31 '12 at 13:47
  • UPDATE Ratemaster, Rates SET Ratemaster.Rate=Rates.Rate WHERE Ratemaster.user=Rates.user ORDER BY Ratemaster.id DESC LIMIT 1 – user1114409 Jan 31 '12 at 13:55
  • Show us some data. Which rows need updating? All of them? (I guess not). All rows of one user? All rows of one user and one product? – ypercubeᵀᴹ Jan 31 '12 at 14:06
  • Ratemaster table has 3 records of user1, all fields have same values, except Rate field which has to be updated from Rates table which has 3 different values in Rate field. How do i copy these 3 values from Rates table to Ratemaster table. Is there any other way of doing this. When UPDATE and WHERE is used, it udpates all the records with only 1 value. – user1114409 Jan 31 '12 at 14:21
  • Ratemaster Table data: Cust_code: 001, Product:Clay, Rate:0, User:user1; Then Rates Table date: Cust_code: 001, Rate:220, User=user1. Like these there are 3 different rates for one customer, product and user, which has to be replaced in Ratemaster from Rates table. Hope this would suffice to understand better. – user1114409 Jan 31 '12 at 14:35

5 Answers5

22

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
)
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Sascha Galley
  • 15,711
  • 5
  • 37
  • 51
  • In such a situation what else should be done to make it work, please help with a solution to my issue. – user1114409 Jan 31 '12 at 13:45
  • added an example query, but I don't know exactly what you want to do. – Sascha Galley Jan 31 '12 at 13:59
  • Hi, The above sql statement works, but the result is, all 3 records meeting user=user of Ratemaster has only 1 value from Rates table, What I need is 3 different values from Rates table, I think the limit is not working, hope the point is clear. – user1114409 Jan 31 '12 at 14:11
  • Hi Sascha, my requirement is simple, I need 3 values of Rates table(Rate) to be copied into Ratemaster field Rate where all 3 records are duplicates except for 1 field Rate column in it. – user1114409 Jan 31 '12 at 14:16
11

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
2

Work It 100%

UPDATE table  SET Sing='p'  ORDER BY sr_no  LIMIT 10;  
1

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.

John Woo
  • 258,903
  • 69
  • 498
  • 492
  • Lol, Just read that article and for a moment I was in shock. The guy described working on a project which seems to be exactly the one I work on ;) – John Oct 03 '16 at 04:09
-6

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;

Karolos
  • 329
  • 1
  • 7
  • Hi, All I need is to update from Table B field1 with Table A field2 where all records are duplicates for one user, need your help to solve this situation, with out limit, UPDATE command updates all the records with same value – user1114409 Jan 31 '12 at 13:49
  • @user1114409: The query you specified will simply do what you told it to (i.e. the content of SET) for ALL fields matching the WHERE clause. What is the layout of your table ? Maybe Ratemaster.user=Rates.user resolves to true more often than you think. – Karolos Jan 31 '12 at 16:39
  • Hi Karolos, Yes you are right the where clause is what i need to limit, so that it does update for only record for matching criteria. In the above question I have clearly given the layout of the table, it is very simple. Check 2nd paragraph for the same. – user1114409 Feb 01 '12 at 05:12
  • @user1114409: Maybe it's simple to you but I don't seem to understand what it is you want after your explanations. You say "I need 3 values of Rates table(Rate) to be copied into Ratemaster field Rate where all 3 records are duplicates except for 1 field Rate column in it"; could you update your post with a few dummy rows as an example ? How do you expect that three _duplicate records_ from ratemaster be filled with three _different_ (?) rates from table rates ? How is the DB supposed to know which of the 3 values from table rates corresponds to those form ratemaster? Sorry I can't figure this – Karolos Feb 01 '12 at 11:38
  • Hi Karlos, **Ratemaster** has : 3 records (id:1, cust_code:1231, Product:Clay, Rate:0, user:user1) (id:1, cust_code:1231, Product:Clay, Rate:0, user:user1) (id:1, cust_code:1231, Product:Clay, Rate:0, user:user1) whereas Table **Rates** table has 3recs : (id:1, cust_code:1231, Rate:299, user:user1) (id:1, cust_code:1231, Rate:355, user:user1) (id:1, cust_code:1231, Rate:466, user:user1), Hope this sample data helps now, I want only Rate field value from Rates table to be inserted into Ratemaster.Rate field. – user1114409 Feb 01 '12 at 12:55
  • This helps a bit but I'm not sure what you expect ? which of 299, 355 or 466 do you want to put in the three fields of ratemaster (replacing the 0s)? If your purpose is to update each row separately and have 299, 355 and 466 for each of the records of ratemaster, then I'm afraid the DB doesn't have enough information to do that, unless you do something like Sascha suggested, but you would have to execute this three times, to select each time the row from ratemaster with Rate=0 (the other would have been updated by the previous updates). – Karolos Feb 01 '12 at 14:26
  • Hi Karlos, I have added 1 more column in Ratemaster called rid where it is unique, it stores the id value of Rates table, will this work ! I am not sure but still trying ! – user1114409 Feb 02 '12 at 05:47
  • Hi Karlos, Yes by adding this rid column in Ratemaster table it is working. mysql_query("UPDATE Ratemaster, Rates SET Ratemaster.Rate = Rates.Rate WHERE Ratemaster.rid=Rates.id "); – user1114409 Feb 02 '12 at 06:42
  • @user1114409: Glad that it worked! I would advise to take a look at designing database schemes (e.g. http://www.databaseanswers.org/tutorial4_db_schema/index.htm) because this is a key part of using databases. For instance, the high amount of duplication between the two tables is suspicious: you have id and rid; what's the difference? – Karolos Feb 02 '12 at 10:38
  • 1
    Limit can be used with "Update" as well: http://dev.mysql.com/doc/refman/5.0/en/update.html – kolinko Aug 07 '13 at 11:07