Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP MYSQL UPDATE WHERE multiple conditions

Tags:

php

mysql

where

I was wondering if you can do an Update with multiple conditions like so:

UPDATE participantes SET confirmado = 1 WHERE id = 19 AND id = 20 AND id = 21;

participantes -> Table

confirmado -> field of the table participantes.

like image 380
user3590264 Avatar asked Dec 04 '22 06:12

user3590264


2 Answers

MySQL's AND clause will only work when ALL of the criteria are met. What you're looking for is OR. In the format that you provided:

UPDATE participantes SET confirmado = 1 WHERE id = 19 OR id = 20 OR id = 21;

Although, the above-noted IN (19, 20, 21) would be better for this specific use case.

Your original query was trying to look for a single row where id was simultaneously 19, 20, and 21, which would never happen.

like image 167
Noah Avatar answered Dec 06 '22 21:12

Noah


To accomplish what you're describing, I would, instead, use the IN clause:

UPDATE participantes SET confirmado = 1 WHERE id IN(19, 20, 21);
like image 43
Aaron Avatar answered Dec 06 '22 21:12

Aaron