What would be the fastest way to delete all records in every table of the database with id = 0
?
It would be simple if every table had its first column called id
, but in my case one table has first column named id_tag
, other table - id_product
, etc.
I've figured out that I can get the name of the first column by:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME = 'ps_tag' LIMIT 1
but how can I include it in a DELETE
query? I need something like:
DELETE FROM 'ps_tag' WHERE [first_column] = 0
My first idea was:
DELETE FROM 'ps_tag'
WHERE (SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = 'dbname' AND TABLE_NAME = 'ps_tag' LIMIT 1) = 0
but obviously it doesn't work.
This is too long for a comment.
Although you can set up a dynamic SQL statement, I often find this type of operation is easier to do in Excel. Write a query to get the column name and table name for the ones you are interested in.
Then, load these into Excel.
In another cell, put in a string like 'delete from @table where @column = 1'
.
Then put in the formula:
=substitute(substitute(<where the string is>, '@table', <tablename>), '@column', <columnname>))
Copy the code back to your database interface and execute it.
(And any spreadsheet will do. I usually have Excel handy.)
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.