1

I was trying to update only 10 records sorted by date in sqlite.

I am using this query but not able to proceed further getting exception at limit.

UPDATE TABLENAME  SET COLUMMANGE = 'VALUE' WHERE  1  LIMIT 10 ORDER BY COLUMNNAME ASC
askedApr 9, 2014 at 7:56
Kartheek Sarabu's user avatar
2
  • Check basics of SQL, please.CommentedApr 9, 2014 at 7:59
  • WHERE is condition so you should write something likeWHERE ID = 1, simplyWHERE 1 is invalid syntax.CommentedApr 9, 2014 at 8:03

3 Answers3

3

I am assuming that you have one field named ID in your table, based on it your query to updated 10 latest record should be done following way,

UPDATE TABLENAME  SET COLUMMANGE = 'VALUE' WHERE ID in ( SELECT ID from TABLENAME ORDER BY COLUMNNAME DESC LIMIT 10 );

Here inner query will select 10 latest record id from the table. These Ids will be passed to outer Update query.

answeredApr 9, 2014 at 8:05
Lucifer's user avatar
Sign up to request clarification or add additional context in comments.

Comments

1

I believe you need an nested query first to fetch the primary keys (or some other key) of the rows you want to update, and than update only those rows. Try something like this:

 UPDATE TABLENAME SET COLUMMANGE='VALUE' WHERE id IN (     SELECT id FROM (         SELECT id FROM TABLENAME         WHERE <YOUR CONDITION>         ORDER BY COLUMNNAME ASC         LIMIT 10     ) );

It is also been discussed here if you want more details:update multiple rows using limit in mysql?.

But really think if you really want this behaviour. Also the WHERE 1 is not a part of the valid SQL syntax .

answeredApr 9, 2014 at 8:04
Aleksandar Stojadinovic's user avatar

1 Comment

WHERE 1 is valid (but superfluous).
1

Try this query

UPDATE TABLE_NAME  SET COLUMN_NAME = 'VALUE' WHERE PRIMARYKEY_COLUMN in ( SELECT PRIMARYKEY_COLUMN  from TABLE_NAME ORDER BY COLUMNNAME DESC LIMIT 10 );
  • PRIMARY KEY COLUMN IS MANDATORY IN THIS TABLE.

  • THE ABOVE QUERY RESULTS TOP 10 RECORDS IN DESCENDING ORDER

answeredApr 24, 2014 at 10:49
Sravana Sandhya's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.