MySQL Delete Records from 2 Tables - mysql

I'm looking to delete information in two different tables in 1 query, based on an ID.
I've tried several solutions on here to accomplish this task but still have not accomplished what I'm trying to do.
Table 1 - Content
---------- ---------
ContentID | Content
--------------------
Table 2 - Votes
---------------------------
VoteID | ContentID | Vote
---------------------------
I want to delete the content row based on its ID and any or all votes (there could be 0 vote records). I do NOT want to use transactions, cascading deletes, or use 2 different queries.
What is best here - a LEFT JOIN? INNER JOIN?
Any help here would be greatly appreciated.

DELETE Content, Votes
FROM Content
LEFT JOIN Votes
ON Votes.ContentID = Content.ContentID
WHERE Content.ContentID = ?

If you have a relation you can try with this ON DELETE CASCADE option.
Another option is to create a stored procedure and do the delete in 2 steps but with only one server call.

DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Although this is for 3 tables, I'm sure you'll be able to adapt it to your needs.

You can specify multiple tables in a DELETE statement to delete rows from one or more tables depending on the particular condition in the WHERE clause. However, you cannot use ORDER BY or LIMIT in a multiple-table DELETE. The table_references clause lists the tables involved in the join. Its syntax is described in Section 12.2.7.1, “JOIN Syntax”.
The first multiple-table DELETE syntax is supported starting from MySQL 4.0.0. The second is supported starting from MySQL 4.0.2.
For the first multiple-table syntax, only matching rows from the tables listed before the FROM clause are deleted. For the second multiple-table syntax, only matching rows from the tables listed in the FROM clause (before the USING clause) are deleted. The effect is that you can delete rows from many tables at the same time and have additional tables that are used only for searching:
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;
Or:
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id AND t2.id=t3.id;
These statements use all three tables when searching for rows to delete, but delete matching rows only from tables t1 and t2.
This link can be useful "http://dev.mysql.com/doc/refman/4.1/en/delete.html"

Without using "JOINS" the simplest I could come up with while I was searching for a solution was DELETE a.*,b.* FROM table1 AS a, table2 AS b WHERE a.id = b.id AND a.field = 1

Related

How to do a join on 2 tables, but only return the data for one table?

I am not sure if this is possible. But is it possible to do a join on 2 tables, but return the data for only one of the tables. I want to join the two tables based on a condition, but I only want the data for one of the tables. Is this possible with SQL, if so how? After reading the docs, it seems that when you do a join you get the data for both tables. Thanks for any help!
You get data from both tables because join is based on "Cartesian Product" + "Selection". But after the join, you can do a "Projection" with desired columns.
SQL has an easy syntax for this:
Select t1.* --taking data just from one table
from one_table t1
inner join other_table t2
on t1.pk = t2.fk
You can chose the table through the alias: t1.* or t2.*. The symbol * means "all fields".
Also you can include where clause, order by or other join types like outer join or cross join.
A typical SQL query has multiple clauses.
The SELECT clause mentions the columns you want in your result set.
The FROM clause, which includes JOIN operations, mentions the tables from which you want to retrieve those columns.
The WHERE clause filters the result set.
The ORDER BY clause specifies the order in which the rows in your result set are presented.
There are a few other clauses like GROUP BY and LIMIT. You can read about those.
To do what you ask, select the columns you want, then mention the tables you want. Something like this.
SELECT t1.id, t1.name, t1.address
FROM t1
JOIN t2 ON t2.t1_id = t1.id
This gives you data from t1 from rows that match t2.
Pro tip: Avoid the use of SELECT *. Instead, mention the columns you want.
This would typically be done using exists (or in) if you prefer:
select t1.*
from table1 t1
where exists (select 1 from table2 t2 on t2.x = t1.y);
Although you can use join, it runs the risk of multiplying the number of rows in the result set -- if there are duplicate matches in table2. There is no danger of such duplicates using exists (or in). I also find the logic to be more natural.
If you join on 2 tables.
You can use SELECT to select the data you want
If you want to get a table of data, you can do this,just select one table date
SELECT b.title
FROM blog b
JOIN type t ON b.type_id=t.id;
If you want to get the data from two tables, you can do this,select two table date.
SELECT b.title,t.type_name
FROM blog b
JOIN type t ON b.type_id=t.id;

MySQL Joins of tables with different columns

I want to create a table that needs to be a combination of selected columns from three or more tables. I don't have any sample data, just added the columns for explanation.
Table 1
A|B1|C1|D1|E1|F1|G1|H1|I1|J1
Table 2
A|B2|C2|D2|E2|F2|G2
Table 3
A|B3|C3|D3|E3|F3|G3
Resultant New table must have
A|B1|E1|F1|G1|J1|C2|D2|G2|B3|D3|F3
I'm not sure if I need to use a FULL JOIN or use UNIONS.
Each of these tables contain more than 400,000 rows. Any help here on what query needs to be included would be really helpful.
You can try the below query:
select t1.A,t1.B1,t3.E1,t1.F1,t1.G1,t1.J1,t2.C2,t2.D2,t2.G2,t3.B3,t3.D3,t3.F3
from table1 t1 join table2 t2 on t1.A = t2.A
join table3 t3 join table2 t2 on t3.A = t2.A
As Palec commented correctly in the other answer, so adding a bit of explanation to the answer.
You need to use the JOINS for this problem instead of UNION. The reason why I am saying to use JOINS over UNION is UNION combines the data/result of two or more queries into a single result set which includes all the rows which exist in the queries in your UNION. But when you are using JOINs, you can retrieve data from two or more tables based on logical relationships between the tables.
Also to add that you should add an alias name to your table so that it becomes easy to retrieve the column in the select query and also while linking the table.
Join is the correct way:
select table1.A,B1,E1,F1,G1,J1,C2,D2,G2,B3,D3,F3 from table1 join table2 on table1.A = table2.A
join table3 join table2 on table3.A = table2.A

Delete referring to multiple table doesn't work on MYSQL

I'm trying to delete from a table, using join from other tables.
My sql is the following:
DELETE FROM `threadsread`
USING `mybb_threads` t
WHERE
threadsread.tid=threads.tid and threadsread.uid in (2111, 2564, 2326, 2510)
and mybb_threads.fid=30
But I get the following error:
1109 - Unknown table 'mybb_threadsread' in MULTI DELETE
None of those are views, all are real tables. And I could run a select using a similar SQL, without any problem.
Please take a look at the below from Mysql.com
For the first multiple-table syntax, only matching rows from the
tables listed before the FROM clause are deleted. For the second
multiple-table syntax, only matching rows from the tables listed in
the FROM clause (before the USING clause) are deleted. The effect is
that you can delete rows from many tables at the same time and have
additional tables that are used only for searching:
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3 WHERE t1.id=t2.id
AND t2.id=t3.id; Or:
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3 WHERE
t1.id=t2.id AND t2.id=t3.id; These statements use all three tables
when searching for rows to delete, but delete matching rows only from
tables t1 and t2.
I managed to get it working with the following sql:
delete FROM threadsread
USING threadsread
inner join threads
WHERE
threadsread.tid=threads.tid and threadsread.uid in (2111, 2564, 2326, 2510)
and threads.fid=30
Thanks for the help M. of CA

Does the table in the FROM clause have to be in the WHERE clause?

I've done a similar JOIN in a UPDATE script, but I used the same table in the SET and WHERE clause. In this DELETE script I need to delete from one table where a condition is true in another table. For example:
DELETE FROM `db_A`.`table_A`
JOIN `db_B`.`table_B`
ON `table_A`.`id` = `table_B`.`id`
WHERE `table_B`.`name` = 'Remove Me'
Can I do something like this?
The MYSQL documentation makes it very clear that yes you can do this
You can specify multiple tables in a
DELETE statement to delete rows from
one or more tables depending on the
particular condition in the WHERE
clause. However, you cannot use ORDER
BY or LIMIT in a multiple-table
DELETE. The table_references clause
lists the tables involved in the join.
Its syntax is described in Section
12.2.8.1, “JOIN Syntax”.
For the first multiple-table syntax,
only matching rows from the tables
listed before the FROM clause are
deleted. For the second multiple-table
syntax, only matching rows from the
tables listed in the FROM clause
(before the USING clause) are deleted.
The effect is that you can delete rows
from many tables at the same time and
have additional tables that are used
only for searching:
DELETE t1, t2 FROM t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
Or:
DELETE FROM t1, t2 USING t1 INNER JOIN t2 INNER JOIN t3
WHERE t1.id=t2.id AND t2.id=t3.id;
These statements use all three tables
when searching for rows to delete, but
delete matching rows only from tables
t1 and t2.
The preceding examples use INNER JOIN,
but multiple-table DELETE statements
can use other types of join permitted
in SELECT statements, such as LEFT
JOIN. For example, to delete rows that
exist in t1 that have no match in t2,
use a LEFT JOIN:
DELETE t1 FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;
The syntax permits .* after each tbl_name for compatibility with Access.
From Delete Manual, the table you want to delete goes between DELETE and FROM. Unless you want to delete from all tables involved in the join
DELETE `db_A`.`table_A` FROM `db_A`.`table_A`
JOIN `db_B`.`table_B`
ON `table_A`.`id` = `table_B`.`id`
WHERE `table_B`.`name` = 'Remove Me'
As a general answer, yes. You can join to the second table with the condition, as you have stated, or pull the ids in a subcommand, like:
DELETE FROM T1
WHERE id in (SELECT id FROM T1 JOIN T2 ON X = Y WHERE T2.Val = "X")
This is a bit heavy weight, but if T2 is the child, you can reduce this to:
(SELECT Y from T2 WHERE Val = "X")
Make sense?

MYSQL select query based on another tables entries

I have stumped on this as I am a total beginner in MySql.
Here is a the basic of how the two tables are formed
Table 1
id,product_id, product_name
Table 2
id,product_id,active
Now i know how to do a select statement to query the results from one table but when I have to involve two, I am lost. Not sure if I have to use inner join, left join etc.
So how can I return the results of the product_id from table 1 only if in table 2 is active?
You could use JOIN (as Fosco pointed out), but you can do the same thing in the WHERE clause. I've noticed that it's a bit more intuitive method than JOIN especially for someone who's learning SQL. This query joins the two tables according to product_id and returns those products that are active. I'm assuming "active" is boolean type.
SELECT t1.*
FROM Table1 t1, Table2 t2
WHERE t1.product_id = t2.product_id AND t2.active = TRUE
W3Schools has a good basic level tutorial of different kinds of JOINs. See INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN.
It's pretty simple to join two tables:
select t1.*
from Table1 t1
join Table2 t2 on t1.product_id = t2.product_id
where t2.active = 'Y'