How to delete from two or multi tables in MySQL
DELETE o,
od
FROM
tb_orders o
JOIN tb_order_detail od ON o.oid = od.oid
WHERE
o.oid = 4
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: