How you can delete from multiple tables with a single mysql query

Hi guys! In this tutorial I will explain to you how you can use mysql to delete from multiple tables. I have created a script for Wordpress where I will delete subscriber users and user_meta to delete all them with a single click.

I have writed a simple mysql query:

$a = 'a:1:{s:10:"subscriber";b:1;}'; // with this variable I will delete only subscribers

Next, I will join wp_users and wp_usermeta and will delete all subscribers.

mysql_query("DELETE users,meta FROM wp_users users JOIN wp_usermeta meta ON meta.user_id = users.ID WHERE meta.meta_value='$a'");

or if you want to delete an user with all informations from wp_usermeta, you can use

mysql_query("DELETE users,meta FROM wp_users users JOIN wp_usermeta meta ON meta.user_id = users.ID WHERE users.ID='$userid'");

In this way you can delete admin or editor users.









Leave a Comment