Specify more than one item in the where clause
-
20-09-2019 - |
Question
Is there any way I could run the following 'logical code' to actually work?
$sql=mysql_query("DELETE FROM users WHERE id='3,4,5,9'");
I basically want to give my user a tick box to tick for all displayed rows, they can then pick which ones to remove. I just want to remove more than one row with the id's specified?
Any ideas?
Solution
What about :
$sql=mysql_query("DELETE FROM users WHERE id IN (3, 4, 5, 9)");
Provided :
- your ids are numeric in the DB
- you want to delete user 3, and delete user 4, and delete user 5, and delete user 9
And, of course, if your ids are strings :
$sql=mysql_query("DELETE FROM users WHERE id IN ('3', '4', '5', '9')");
For more informations, see :
IN()
- And, why not : Subqueries with ANY, IN, and SOME
OTHER TIPS
You can use the IN operator for that.
DELETE ... WHERE id IN (3,4,5,9)
Use a WHERE IN
clause.
DELETE FROM users WHERE id IN (3, 4, 5, 9);
Have you tried using IN
DELETE FROM users WHERE id IN (3,4,5,9)
Although in my applications I never delete anything. Instead I have an active flag that I set to false.
UPDATE users set active=0 where id in (3,4,5,9)
All queries then have a where clause for active=1
and 1 is the default value for the active flag in the table.
You can use in
instead of =
.
E.g.,
DELETE FROM users WHERE id IN (3,4,5,9);
$sql=mysql_query("DELETE * FROM table WHERE id IN (3,4,5,9)");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow