vendredi 27 février 2015

How to update multiple rows (not ever row) with same value in SQLite database

I have a strange problem with a scenario related to SQLite update, hopefully will get some help here.


my table tbl1 looks like below (sqlfiddle)



id col1 col2 col3
1 0 1 0
2 1 0 0
3 0 0 1
4 1 1 1
5 0 0 0


My requirement is that I get some positive number from user for each column and I need to update the rows of a particular column if they contain zero and finally I should count the number of rows updated and difference between the user input and updated count I should add it to the last row


Let me explain with example:

Say user inputs 10 for col1. col1 has 3 rows containing zero so I will update them to 1 and finally the diff i.e. (10 - 3 = 7) I should update in the last row


I hope I did not confuse, after update I expect the table to look like



id col1 col2 col3
1 1 1 0
2 1 0 0
3 1 0 1
4 1 1 1
5 8 0 0


Sql query update tbl1 set col1=1 where col1=0 and id in (select id from tbl1 where col1=0 limit 10)


The above query updates all 0s to 1 but how do I add the remaining to last row?


Aucun commentaire:

Enregistrer un commentaire