Jump to content

wild card delete


EchoFool

Recommended Posts

Hey,

 

Quick question - is there a way to build a query so that it will delete all rows of data from any database / tablename from my server that has the field "UserID" and "UserID = 0" ?

 

Or do i have to be more precise in my MYSQL to do it which im hopeing not.

Link to comment
Share on other sites

Nope. You have to delete it from each table using separate query.

 

Alternatively, if you had proper constraints in place, MySQL would do all the work for you, but since you're asking this question, you probably don't have them.

Link to comment
Share on other sites

Here's manual entry on table constraints:

http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

 

They can be defined so that when you delete a row from 'parent' table (e.g. 'users') all rows in 'child' tables (for example 'messages') will be deleted automatically.

 

 

Anyway:

SELECT CONCAT(TABLE_SCHEMA,'.',TABLE_NAME) FROM `information_schema`.`COLUMNS` WHERE COLUMN_NAME = 'UserID';

 

will fetch you a list of all tables having 'UserID' column. Then you just need to run DELETE query on each of them. You should be able to do this in, say, 25 lines of PHP

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.