Header Ads

How to delete from two or multi tables in MySQL

This tutorial will show you how to delete from two or multi tables in MySQL we will also use join to id in the first table and second table.

DELETE o,
 od
FROM
 tb_orders o
JOIN tb_order_detail od ON o.oid = od.oid
WHERE
 o.oid = 4

another example

SELECT * FROM `users` AS u LEFT JOIN `user_posts` AS p ON u.id p.author WHERE u.id? LIMIT 1;

SELECT * FROM `cannabis` LEFT JOIN `users` ON `cannabis`.`user_id` `users`.`id` WHERE `cannabis`.active1 AND `cannabis`.deleted0 ORDER BY `cannabis`.created DESC LIMIT 0, 30;

SELECT * FROM `cannabis` LEFT JOIN `grow_notes` ON `cannabis`.`id` `grow_notes`.`cannabis_id` WHERE `grow_notes`.user_id 1 AND `grow_notes`.note LIKE '%mySQL%' ORDER BY `cannabis`.date DESC LIMIT 0, 10;

SELECT

`c`.`id`,

`a`.`name` AS `author_name`,

COUNT(DISTINCT `b`.`id`) AS `books_count`

FROM

`book` AS `a`

SELECT * FROM table_a JOIN table_b ON table_a.id table_b.id WHERE table_a.id? AND table_b.id?;

Learn more about join 

1. Join

A join combines rows from two tables into a single row based on shared values between them. This is done using the JOIN keyword.

2. Inner Join

An inner join combines records that have matching values in both tables.

3. Left Join

A left join combines records from the first table with no matching value in the second table.

Why do we use join ?

1. Join combines rows from two tables into a single row.

2. In SQL, a JOIN operation combines information from two or more tables based on common columns.

3. A JOIN operation can be performed between any number of tables.

4. Joins are often used to combine data from multiple tables that share at least one column.

5. You can use the INNER JOIN syntax to perform a JOIN operation between two tables.

6. You can use the LEFT OUTER JOIN syntax to perform an outer join between two tables.

7. Join combines rows from two tables into one row. This is done using a condition that matches records between the two tables.

8. In this example, we are joining table tbl_users with tbl_categories. We want to get all users who have a category of music.

SELECT u.* FROM tbl_users AS u LEFT JOIN tbl_categories AS c ON (u.category c.id) WHERE c.name'music'

No comments:

Powered by Blogger.