Jump to content

MySQL error. Trying to DELETE using a subquery


micah1701

Recommended Posts

I have a table called "Calendar"
some of the fields are:

+-----+-------------+-----------+
|  id  | recurrenceOf |  date      |
+-----+-------------+-----------+
|  1  |                  | 20050603 |
+-----+-------------+-----------+
|  2  |      1          | 20050604 |
+-----+-------------+-----------+
|  3  |      1          | 20050605 |
+-----+-------------+-----------+

I want to delete all rows before a given date except, I do NOT want to delete a row that has other rows linked to it.

so, in the above example, I want to keep row 1 but delete the other two rows.

This works to return the correct results that I want to delete:
[code]
SELECT * FROM calendar AS rowData
WHERE startDateStamp < '$saveDate'
AND
( SELECT COUNT(*) FROM calendar
    WHERE recurrenceOf = rowData.id
    AND startDateStamp > '$saveDate'
  ) = 0
[/code]
BUT when I change "SELECT * FROM" to "DELETE FROM" it fails and throws a syntax error.

[b]EDIT:
"its okay to use a subquery for assignment within an UPDATE statement, since subqueries are legal in UPDATE and DELETE statements as well as in SELECT statements. However, you cannot use the same table for both the subquerys FROM clause and the update target."

GREAT, SO ANY OTHER IDEAS HOW I CAN DO THIS IN ONE QUERY?[/b]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.