Delete data of product_id from whole database - php

I have data of products in many tables. and I want to delete all this data by a product_id from their tables. But don't want to use many queries.
For Example
1. delete from tbl_product_attributes where product_id = 'this'
2. delete from tbl_product_barcode where product_id = 'this'
3. delete from tbl_product_images where product_id = 'this'
4. delete from tbl_product where product_id = 'this'
I just want a one query that delete all my relative data of specific given product_id from a database. just like this
Delete data from whole DB where product_id = 'this'.
Note: And also this query not burden on my server.
Is there any way?

You can try like this:
DELETE T1, T2
FROM T1
INNER JOIN T2 ON T1.key = T2.key
WHERE condition;
Also, you can define foreign key constraints on the tables with ON DELETE CASCADE option.
Then deleting the record from parent table removes the records from child tables.
Check this link : Mysql delete

Related

Delete from multiple tables having common field

Hi I have two tables as per following.....
tbl_favorite_properties
1 favorite_properties_id
2 favorite_properties_property_id
3 favorite_properties_user_id
tbl_property
1 property_id
2 property_user_id
Now i want to delete fields based on user which is straight forward as below
$user_id = $_SESSION['user_id'];
$delete_tbl_favorite_properties = $con->prepare("DELETE FROM tbl_favorite_properties WHERE favorite_properties_user_id='$user_id'");
$delete_tbl_favorite_properties-> execute();
$delete_tbl_property = $con->prepare("DELETE FROM tbl_property WHERE property_user_id='$user_id'");
$delete_tbl_property-> execute();
Now I want to delete all properties in tbl_favorite_properties (favorite_properties_property_id) which match this users properties in tbl_property (property_id)
I am already able to do this in innodb cascade delete using mysql, but need a php solution.
there is a solution here How do I delete row with primary key using foreign key from other table?
but there the column names are the same and mine are different...
i am new to structuring queries in multiple tables...
I think the behavior you want is to remove properties in tbl_property belonging to a certain user, but then to also delete the corresponding favorites in tbl_favorite_properties:
DELETE t1, t2
FROM tbl_property t1
LEFT JOIN tbl_favorite_properties t2
ON t1.property_id = t2.favorite_properties_property_id
WHERE t1.property_user_id = '$user_id'
What threw me off initially is that both tables have a user_id column. You might not need the user_id in tbl_favorite_properties if you always plan to enter that table via a join from tbl_property.

DELETE FROM multiple tables?

Anyone have an idea on how to delete from multiple tables in one query? I have been scratching my head endless trying to find a way to make this work. But my endless search on google havnet helped..
I have a delete button on my site that needs to delete from multiple tables.. Right now, it uses the code to delete from the the table "tilbehor"
DELETE FROM tilbehor WHERE t_id = '$id'
But I also need it to delete from the table "kategori"
In the kategori table i have the 3 columns "id", "p_id" and "t_id". I need it all to be deleted where "t_id" = the $id from the query
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.t_id=t2.t_id AND t2.t_id=t3.t_id;
If the t_id is the main ID. You could look into Foreign keys and use on DELETE Cascade. Foreign keys are connected with a primary key from another table. When the matching primary key of the table is deleted, the rows on other tables that match the foreign key with the primary key are deleted as well

Delete data from more tables in one query

Please, could you help me, how to delete data from more than 1 table in just one query?
I have this tables structure:
products
texts
files
products_files - here are mapped files to products - product_id, file_id
texts_files - here are mapped files to texts - text_id, file_id
And I need to do this:
If I delete file with id 50, I need to delete all rows from products_files and texts_files where file_id = 50.
Do you know, how to do it?
I tried to use left join, but without any results...
$query = 'DELETE products_files, texts_files FROM products_files
LEFT JOIN texts_files ON texts_files.file_id = products_files.file_id
WHERE products_files.file_id = '.$id.' OR texts_files.file_id = '.$id.'';
You might need ON DELETE CASCADE functionality. However, use it with care as it might have surprising consequences:
http://www.mysqltutorial.org/mysql-on-delete-cascade/
If you add ON DELETE CASCADE in your create table-query, you can delete any item from it. If there are any foreign key constraints, mysql automatically follows the connection and also deletes entries in the other table.
If you wanna stay on the safe side though (recommended), you will need to use multiple delete statements.
you can specify which table to delete from by prefixing a * with the table name: DELETE table.* FROM table1 JOIN table2 JOIN ... WHERE ... The WHERE clause specifies which rows to delete.
Try this:
DELETE f1,f2
FROM products_files f1
INNER JOIN texts_files f2 ON f1.file_id = f2.file_id
WHERE f1.file_id = '.$id.';
OR
use MySQL Foreign Key constraints
MySQL foreign key constraints, cascade delete
Essentially your query seems fine to me (except for the LEFT JOIN) - does this not work...
DELETE pf
, tf
FROM products_files pf
JOIN texts_files tf
ON tf.file_id = pf.file_id
WHERE pf.file_id = $id;

PHP Delete row from table1 if row doesn't exist in table2

I have two tables, one is where the actual posts are, and one for posts that are saved.
I want to delete a row from the saved table if a post is deleted from the website
I tried this:
mysql_query("DELETE FROM `saved` WHERE (SELECT * FROM `ads` WHERE ad_id = `saved`.ad_id) LIMIT 1");
but this doesn't work. Can't think of how to do it the right way
any help it appreciated!
This will delete all rows from saved which its saved.ad_id do not reference to any ads.ad_id
delete s
from saved AS s
left outer join ads as a
on a.ad_id=s.ad_id
where a.ad_id is null;
More here: MySql delete syntax
And here: MySql Multi delete example
you can use left outer join to get the rows exist in table 1 and not exist in table 2 then you can delete it from table 2 using their IDs
mysql_query("DELETE FROM `saved` WHERE `saved`.ad_id not in (SELECT ad_id FROM `ads`)");

php mysql code to delete data from multiple table having same id

I have a form from where Information is entered into multiple database having same id, if by mistake wrong data is entered, I want to delete that from all tables.
How and what should I do to delete from all information related to that id from all tables.
You can do multiple table deletes:-
http://dev.mysql.com/doc/refman/5.0/en/delete.html
For example say you had 4 tables and want to delete all the records in 3 of them that relate to the 1st table:-
DELETE Table1, Table2, Table3
FROM Table0
INNER JOIN Table1
ON Table0.Id = Table1.ParentId
INNER JOIN Table2
ON Table0.Id = Table2.ParentId
INNER JOIN Table3
ON Table0.Id = Table3.ParentId
WHERE Table0.Id = 1
Of course you could also delete from the first table (Table0) as well.

Categories