I’m learning SQL
and I try some things in phpMyAdmin
like creating or deleting tables, creating or deleting records and so on…
Let’s assume I have a table where there are 2 columns – id
and name
populated with 7 records. Now I want to delete some records whose id is 5 and 6.
My query is like this:
DELETE FROM table_name WHERE id = 5 AND id = 6
This query isn’t working. But if I’ll write DELETE FROM table_name WHERE id = 5
without AND condition, it will work. Am I do something wrong?
2
Answers
Think about the logic of what you are saying. That query says that the row must have an
id
that simultaneously equals 5 and 6, which we know is impossible.You could have
column1 = stack
andcolumn2 = overflow
, because those are two columns and thus could have different values, but the same column cannot contain multiple values in a single row.So instead, try using the
OR
operator. Which has logic like: Delete the row from the table where the ID equals this or the row in the table equals thatLike this:
If you are using AND in condition then mysql will match or satisfy both conditions which is impossible for single row in your case to match it so instead you should try:
DELETE FROM table_name WHERE id = 5 OR id = 6;
OR
DELETE FROM table_name WHERE id IN (5,6);
You can try any of the above two query.