简体   繁体   中英

Deleting multiple rows from MYSQL table based on column data in PHP

I have a table called user_favs like this :

 id | user_id | post_id
 ----------------------
 50 | 291     | 1027
 51 | 10      | 180
 52 | 771     | 1027
 53 | 92      | 133
 54 | 523     | 1027

How would I set up a database query in php to delete all rows that contain post_id 1027?

I know how to do it for single entries :

DELETE FROM user_favs WHERE post_id = 1027

But I don't know how to delete all rows that contain post_id 1027.

Is it something like :

DELETE * FROM user_favs WHERE post_id = 1027

I don't want to try it without knowing in case I mash the whole thing up ;)

this:

DELETE FROM user_favs WHERE post_id = 1027

will actually not delete only one row. it will delete all rows that matches the criteria post_id=1027

I am pretty sure that the first statement will work. Either way, I would recommend you to do a backup of your db before doing any change, even more, I would recommend you to do this type of trials in a vagrant box or in a database you dont care as the one you could create in your localhost. instead of deleting, to check your code you could do: SELECT * FROM user_favs WHERE post_id = 1027 and see if it worked, if the results are the tuples you want to delete, whoala!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM