tags:

views:

58

answers:

5

Hey everyone,

Im 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, thank you

A: 
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.

Trefex
+2  A: 

If you have a relation you can try with the 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.

munissor
I don't think that this satisfies the *"I do NOT want to use transactions, cascading deletes, or use 2 different queries"* clause ;-)
Mike
Maybe only the stored procedecure part, if the concern is having only 1 server request... I admit that i skipped the "requisites" part of the answer ;)
munissor
+1  A: 

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"

So what would be my WHERE clause here? The contentId exists in the content table, but also may or may not exist in the votes table 1 or more times.
barfoon
DELETE Content, Votes FROM Content INNER JOIN Votes where Content.ContentID = Votes.ContentID and "apply your condition"
@dilipchouhan: An inner join will prevent content records from being deleted if no matching rows are found in votes. It should be a left join: `DELETE Content, Votes FROM Content LEFT JOIN Votes ON Votes.ContentID = Content.ContentID WHERE Content.ContentID = ?`
Mike
DELETE Content, Votes FROM Content INNER JOIN Votes where Content.ContentID = Votes.ContentID AND Content.ContentID = 999. This wont work as it wont delete the record from the content table when there are no votes - "where Content.ContentID = Votes.ContentID" is then not satisfied.
barfoon
@Mike - your query worked. Deleted from both tables when there were no votes, or 1 or more. If you post that as an answer I will accept.
barfoon
@Mike : Thanks, By mistake i used inner join . You are write.
+1  A: 
DELETE Content, Votes
FROM Content
LEFT JOIN Votes
ON Votes.ContentID = Content.ContentID
WHERE Content.ContentID = ?
Mike
A: 

Hi , Try This

DELETE FROM AccountsToUser

FROM AccountsToUser AS AU INNER JOIN

Accounts as A ON a.AccountId = au.AccountId

WHERE a.OwnedByAccountId in (select AccountId from Accounts where OwnedByAccountId is null)

gsoni