mysqlsqlsql-updatemysql-select-db

SQL UPDATE boolean from a db selecting a minium value of number WHERE boolean=false


I want to UPDATE my database from a previous selection. I mean I need to select the minium value of the register when the boolean=false and then change the value of the boolean of that record to true. They are all from the same table.

numberColumn    booleanColumn
     1                1                   
     2                1
     3                0
     4                0

Result need to be this:

numberColumn    booleanColumn
     1                1                   
     2                1
     3                1
     4                0

The statement im using is like this and there is probably an error on the syntax.

"UPDATE db(booleanColumn) SELECT MIN(numberColumn) FROM db WHERE boolean='0' SET boolean='1'"

Im feeling like I need to nest something like:

"UPDATE db(booleanColumn) 
SET boolean='1'
WHERE (SELECT MIN(numberColumn) WHERE boolean=0)"

but if not working also. I dont know if you are getting the question or I need to express myself better. Thank you from now,


Solution

  • In MySQL, you can do:

    update db.table
        set boolean = 1
        where boolean = 0
        order by numberColumn
        limit 1;