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
Related
I have 3 tables in a mysql database, I am wanting to delete from 2 of them via a JOIN based on the condition that a 3rd table doesn't have any ID's remaining that pertain to another record.
So far I have,
DELETE t1, t2
FROM t1
INNER JOIN t2 ON t1.qs_id = t2.qst_qs_id
What I can't work out is how to check the records of t3 to make a where clause work. The scenario I am trying to make work is as follows,
delete from t1 and t2, for any qs_ids that don't have any rows remaining in t3 table t3 has a column called qsa_qs_id, so if there are no rows matching the ids from the JOIN I can run the delete, but I cannot figure out how to put that into my WHERE.
PSEUDO would be,
DELETE from t1, t2, all rows that have matching qs_id columns BUT only do it if t3 doesn't have any rows with those qs_id remaining.
DELETE t1, t2
FROM t1
INNER JOIN t2 ON t1.qs_id = t2.qst_qs_id
WHERE t1.qs_id NOT IN (SELECT t3.qs_id FROM t3);
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
I am using mySQL 5.6. I have two tables: t1 and t2. Both have many columns. And many columns in t1 and t2 share the same name: for example, there is a "var1" column in t1 and in t2.
I want to join the tables, selecting (a) all columns from t1 and (b) only the columns in t2 that have names that don't appear in t1. For example, I would not select "var1" from t2.
Here is a valid mySQL command that does not work because some columns share the same name:
SELECT * FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
MySQL sensibly returns this error message:
ERROR 1060 (42S21): Duplicate column name 'var1'
So I want to run a command like
SELECT t1.*, DISTINCTCOLUMNS(t2.*) FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
Except, of course, that there is no DISTINCTCOLUMNS operation in SQL. But is there a similar (real) command that will achieve the same effect?
I see that common advice is to avoid SELECT * syntax, partly for efficiency purposes. I appreciate that, but I do not want to write out the names of all of the columns that I need.
The real issue here is you have the same column name for both tables. So you need to alias your columns.. This is also why you shouldn't just pull all columns out but the specific ones you need..
SELECT t1.ID as t1_id,
t1.var1 as t1_var1,
t2.ID as t2_id,
t2.var1 as t2_var1,
... Etc.
FROM t1
LEFT JOIN t2 on t1.ID = t2.ID
With two ID columns and no way to distinguish between the two an error will occur
You could qualify the columns like
SELECT t1.*, t2.* FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
But you should not do it unless the columns with identical names do actually hold/reference the same data.
try like following. this may help you
SELECT t1.*, t2.YourDesiredColumnName FROM t1 LEFT JOIN t2 ON (t1.ID=t2.ID);
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?
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