473,421 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

DELETE and LEFT JOIN problem

Gotta post because this is driving me nuts.

Trying to DELETE orphans. I can successfully:

SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE
UsersID IS NULL;

but when I try:

DELETE FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE UsersID
IS NULL;

I get:

ERROR 1064: You have an error in your SQL syntax near 'LEFT JOIN Users
ON UsersID = UserID WHERE UsersID IS NULL' at line 1

I can also successfully:

DELETE FROM Groups WHERE GroupID = '<specific>';

I have been trying several different methods, but a little hesitant to
go to far, considering I'm DELETEing. I have been checking out past
postings, but nothing that fits this situation.

Any help on this sql would be greatly appreciated. I'm using mysql
version 3.23.55.

Thanks.
Jul 19 '05 #1
2 16719
BDR
http://www.mysql.com/doc/en/DELETE.html

From this, one can quickly deduce that you cannot use a join with a
delete. (Joins are for selecting).

If you want a multi-table delete, then read into the documentation and
you'll find that this wasn't implemented until version 4.x.

From http://www.mysql.com/doc/en/DELETE.html:

Quote-

The first multi-table delete format is supported starting from MySQL
4.0.0. The second multi-table delete format is supported starting from
MySQL 4.0.2.

The idea is that only matching rows from the tables listed before the
FROM or before the USING clause are deleted. The effect is that you can
delete rows from many tables at the same time and also have additional
tables that are used for searching.

The .* after the table names is there just to be compatible with Access:

DELETE t1,t2 FROM t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id

or

DELETE FROM t1,t2 USING t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id

In the above case we delete matching rows just from tables t1 and t2.

If an ORDER BY clause is used (available from MySQL 4.0.0), the rows
will be deleted in that order. This is really only useful in conjunction
with LIMIT. For example:

DELETE FROM somelog
WHERE user = 'jcole'
ORDER BY timestamp
LIMIT 1
- End Quote

So, the solution to your problem is to run two queries:

1. SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID
WHERE UsersID IS NULL;

Save GroupID and pass it on to the next one(s):

2. Delete from Groups where etc...

Cheers.

michael wrote:
Gotta post because this is driving me nuts.

Trying to DELETE orphans. I can successfully:

SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE
UsersID IS NULL;

but when I try:

DELETE FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE UsersID
IS NULL;

I get:

ERROR 1064: You have an error in your SQL syntax near 'LEFT JOIN Users
ON UsersID = UserID WHERE UsersID IS NULL' at line 1

I can also successfully:

DELETE FROM Groups WHERE GroupID = '<specific>';

I have been trying several different methods, but a little hesitant to
go to far, considering I'm DELETEing. I have been checking out past
postings, but nothing that fits this situation.

Any help on this sql would be greatly appreciated. I'm using mysql
version 3.23.55.

Thanks.


Jul 19 '05 #2
BDR
http://www.mysql.com/doc/en/DELETE.html

From this, one can quickly deduce that you cannot use a join with a
delete. (Joins are for selecting).

If you want a multi-table delete, then read into the documentation and
you'll find that this wasn't implemented until version 4.x.

From http://www.mysql.com/doc/en/DELETE.html:

Quote-

The first multi-table delete format is supported starting from MySQL
4.0.0. The second multi-table delete format is supported starting from
MySQL 4.0.2.

The idea is that only matching rows from the tables listed before the
FROM or before the USING clause are deleted. The effect is that you can
delete rows from many tables at the same time and also have additional
tables that are used for searching.

The .* after the table names is there just to be compatible with Access:

DELETE t1,t2 FROM t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id

or

DELETE FROM t1,t2 USING t1,t2,t3 WHERE t1.id=t2.id AND t2.id=t3.id

In the above case we delete matching rows just from tables t1 and t2.

If an ORDER BY clause is used (available from MySQL 4.0.0), the rows
will be deleted in that order. This is really only useful in conjunction
with LIMIT. For example:

DELETE FROM somelog
WHERE user = 'jcole'
ORDER BY timestamp
LIMIT 1
- End Quote

So, the solution to your problem is to run two queries:

1. SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID
WHERE UsersID IS NULL;

Save GroupID and pass it on to the next one(s):

2. Delete from Groups where etc...

Cheers.

michael wrote:
Gotta post because this is driving me nuts.

Trying to DELETE orphans. I can successfully:

SELECT GroupID FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE
UsersID IS NULL;

but when I try:

DELETE FROM Groups LEFT JOIN Users ON UsersID = UserID WHERE UsersID
IS NULL;

I get:

ERROR 1064: You have an error in your SQL syntax near 'LEFT JOIN Users
ON UsersID = UserID WHERE UsersID IS NULL' at line 1

I can also successfully:

DELETE FROM Groups WHERE GroupID = '<specific>';

I have been trying several different methods, but a little hesitant to
go to far, considering I'm DELETEing. I have been checking out past
postings, but nothing that fits this situation.

Any help on this sql would be greatly appreciated. I'm using mysql
version 3.23.55.

Thanks.


Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Marek Lewczuk | last post by:
Hello, I have a strange problem, maybe some of you will be able to explain me something. I use LEFT JOIN as a substitute for subselects. It's true that many subselects can be rewriten using LEFT...
0
by: Oliver Spiesshofer | last post by:
Hi, I want to delete items from a table that are determined with a left join. using DELETE ( SELECT * FROM item_colors LEFT JOIN items ON item_colors.item_id = items.item_id WHERE...
5
by: Marek Kotowski | last post by:
In MySQL online documentation there are some examples with multi-tables left joins. But all of them are like this (taken from the documentation): SELECT ... FROM table1 LEFT JOIN table2 on...
2
by: Bruce Duncan | last post by:
I'm a bit new to MySQL (know MS SQL well...and that may be the problem...getting the syntax confused) and I'm having a join problem...can anyone offer some help? Here's my problem: I have table1...
2
by: R. Tarazi | last post by:
Hello, DB-structure: ========= firmendaten ----------------- firmendatenid <- firmennummer name strasse
2
by: Ryan | last post by:
I'm going daft. I have what should be a simple query and it seems that the left side of the join is being ignored. The query and DDL are below. Basically, my RDOData_Extract_Lines table (where...
1
by: Chris Thompson | last post by:
Here is the problem. I have a table called Locations with a field called Location which holds a list of peoples names. I have a query (query1 say)which has two fields :- 1st field is a person's...
9
by: Alan Lane | last post by:
Hello world: Background: Yesterday, January 21, Doug Steele was kind enough to help me out on a Left Join problem. I was trying to return all stores and their Gross Adds for December, 2004...
4
!NoItAll
by: !NoItAll | last post by:
Total SQL Newbie here! I have two tables I need to join. There are 34-thousand entries in table 1, 5-thousand entries in table 2. So - there are only 5-thousand records that need to be joined ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.