DELETE FROM multiple tables? - php

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

Related

Delete data of product_id from whole database

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

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.

Select two tables when one table is empty or not empty

i have stucked to figuring out what's wrong with my query. i select table 'acara' and table 'eventorg',
if table 'acara' not found or empty,
table 'eventorg' dont want to show up,
but if table 'acara' not empty or found,
table 'eventorg' and table 'acara' will show up
it is my query
SELECT
eventorg.nama as NamaEO,
eventorg.deskripsi as DeskEO,
eventorg.logo as LogoEO,
eventorg.email as EmailEO,
eventorg.telp as TelpEO,
acara.nama as NamaEvent,
acara.id_acara,
acara.tanggal,
acara.deskripsi,
acara.lokasi
FROM eventorg
LEFT JOIN acara
ON acara.id_eo=eventorg.id_eo AND eventorg.id_eo='$ideventO';
'id_eo' in table 'eventorg' is primary key, 'id_eo' in table 'acara' is foreign key
If you want to select every row of eventorg regardless of whether a corresponding record in acara exists, then you need a RIGHT JOIN, not a LEFT JOIN (or reverse the tables in your FROM and LEFT JOIN clauses).
As Matthew referenced in his comment, Jeff Atwood's Venn diagrams are nice way to visualize it: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/

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;

Delete from two table using mysql in php

I have two table category and subcategory
I am trying to delete record from this table using mysql query
Query is=
$sql="Delete t1, t2 From category as t1
INNER JOIN subcategory as t2 on t1.c_id = t2.c_id
WHERE t1.c_id='$del_c_id' ";
This query only delete row from category(t1) whose primary key used into subcategory(t2) table.
Not delete row from category(t1) whose primary key not used in subcategory(t2).
You need to use a LEFT JOIN instead of INNER JOIN. By definition LEFT JOIN returns all results from t1, even if they don't have a match in t2.
See this link for more info on join types:
http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html
Try CASCADE option in mysql. Subcategory will be deleted automatically if you delete the category. You need to use the InnoDB storage engine to use this feature.
Here is the accepted answer

Categories