Results 1 to 8 of 8

Thread: [RESOLVED] SQL - DELETE statement with JOINS in it

  1. #1

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Resolved [RESOLVED] SQL - DELETE statement with JOINS in it

    Hi all,

    I have these database tables in a MySQL database (version 5.0.67-community):

    quotes (fields: id, customer_info_id, vehicle_id)
    customer_info (fields: id, first_name, last_name, address_id)
    addresses (fields: id)
    vehicles (fields: id)

    One quote = 1 record in each of those tables, all linked.

    What I want to do is delete all records from all tables if the first or last name of the customer is 'test'. It seems too complicated to do it all in one query so I've tried doing one query per table like this:

    Code:
    DELETE FROM
      vehicles b
    LEFT JOIN
      quotes a ON a.vehicle_id = b.id
    LEFT JOIN
      customer_info c ON a.customer_info_id = c.id
    WHERE
      c.first_name = 'test' OR
      c.last_name = 'test'
    But the MySQL server gives a syntax error on that.

    Any help is appreciated, cheers!
    Chris

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SQL - DELETE statement with JOINS in it

    I think you need to look at Cascading Deletes.

    Does MySQL support that?

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL - DELETE statement with JOINS in it

    What about using and In Statement for the delete?
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: SQL - DELETE statement with JOINS in it

    DELETE statements don't contain joins. Only SELECT statements can contain joins. You can create a DELETE statement that contains a subquery in the WHERE clause. That subquery would be a SELECT statement and it can contain a join. That's basically what Gary is saying, e.g.
    SQL Code:
    1. DELETE Child
    2. WHERE ParentID IN
    3. (
    4.     SELECT Parent.ID
    5.     FROM Parent
    6.     INNER JOIN Grandparent
    7.     ON Parent.GrandparentID = Grandparent.ID
    8.     WHERE Grandparent.Name LIKE 'T%'
    9. )
    You delete every child whose grandparent's name starts with T.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: SQL - DELETE statement with JOINS in it

    Solved it I think, does anyone see any problems with this query? It seems to be working as expected:

    Code:
    DELETE
      vehicles,
      customer_info,
      addresses,
      quotes
    FROM
      quotes
    LEFT JOIN
      vehicles ON vehicles.id = quotes.vehicle_id
    LEFT JOIN
      customer_info ON customer_info.id = quotes.customer_info_id
    LEFT JOIN
      addresses ON addresses.id = customer_info.address_id
    WHERE
      customer_info.first_name = 'test' OR
      customer_info.last_name = 'test'
    Chris

  6. #6

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: SQL - DELETE statement with JOINS in it

    Thanks jmcilhinney, GaryMazzone and Hack for the assistance. I might look into the IN statement some other time, the joins seem to be working for me.
    Chris

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [RESOLVED] SQL - DELETE statement with JOINS in it

    Well I'll be b*ggered! I didn't even know that such a statement was possible. What database are you using?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Frenzied Member the182guy's Avatar
    Join Date
    Nov 2005
    Location
    Cheshire, UK
    Posts
    1,473

    Re: [RESOLVED] SQL - DELETE statement with JOINS in it

    jmcilhinney, this is a MySQL database. You might be thinking of MS SQL Server?
    Chris

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width