laPistola Posted July 3, 2011 Share Posted July 3, 2011 MySQL 5.1 Is there a command that can find every table that contains a given field name and then delete every row that contains a given value within that field. I know the below don't work (after trying) but it may help explain what im look at doing. DELETE FROM * WHERE id = 1 Thank you. Quote Link to comment Share on other sites More sharing options...
sunfighter Posted July 3, 2011 Share Posted July 3, 2011 It's mysql_query("DELETE FROM 'table_name' WHERE 'column_name'='what_your_looking_for'"); Quote Link to comment Share on other sites More sharing options...
fenway Posted July 4, 2011 Share Posted July 4, 2011 Across tables? I suppose you could use the information_schema to determine which tables contain a given field -- but why on earth would you want to do this? Quote Link to comment Share on other sites More sharing options...
laPistola Posted July 4, 2011 Author Share Posted July 4, 2011 Im writing some software for a training company and one of the features is to delete a student from the system. Now just deleting a student from the students DB table would cause errors as the information regarding what course they was on and which units had been assigned to them and IV forms and list goes on. All this would still be in the DB and come up on some pages within the application, but when clicked on an error would appear as it wouldn't be able to find the student in the DB. So instead of doing a few queries to target and delete the rows that contain there id from each table, I wondered if there was a faster way not just to write it (this way would only take 5 mins to code anyway) but server load times. There are a few times where a command such as I have inquired about would be an advantage. Mostly when deleting information. Quote Link to comment Share on other sites More sharing options...
laPistola Posted July 4, 2011 Author Share Posted July 4, 2011 So if im right you are saying the only real way would be to run a SELECT command on the COLUMNS table within the information schema eg SELECT TABLE_NAME FROM COLUMNS WHERE COLUMN_NAME = 'sid' Then run a loop that does a query on each table_name returned? If so this would run one more query then if I just manually told the script which tables to target. Quote Link to comment Share on other sites More sharing options...
ebmigue Posted July 4, 2011 Share Posted July 4, 2011 So if im right you are saying the only real way would be to run a SELECT command on the COLUMNS table within the information schema eg SELECT TABLE_NAME FROM COLUMNS WHERE COLUMN_NAME = 'sid' Then run a loop that does a query on each table_name returned? If so this would run one more query then if I just manually told the script which tables to target. Would do the same thing. The advantage of doing the deletion per table on a loop than by manually doing it, is that the whole functionality is automated. If in the future, a new table with the field(s) of your interest is introduced, you need not edit your script. Quote Link to comment Share on other sites More sharing options...
laPistola Posted July 4, 2011 Author Share Posted July 4, 2011 Yes I was thinking about doing it anyway to make the software more flexible with future expansion. Even more so to prevent more tables getting created that stores the sid and I or someone forgets to add it to the delete scripts. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.