SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    delete from db with a table join.

    I have a parent table and a child table.

    For my delete, the details I need to relate to are spread across the two tables.

    parent table holds the business id and content_category whilst the child table stores the relevant dates.

    does this query structure (with a sub-select), fit the bill or is there a simpler more efficient way? Is there a principle that can apply to the decision-making process?

    I dislike asking for help with vaguely or quite similar queries when I reckon there must be a rule or principle which 'dictates' key criteria with regard to sub selects or specific joins etc.

    Code MySQL:
    delete from file_sequencer
          WHERE file_id IN ( SELECT file_id 
                               FROM file_sequencer as fs
                           INNER 
                               JOIN file_dates as fd
                                 ON fd.file_id = fs.file_id
                              WHERE fs.business_id = ?
                                AND fs.content_category = ?
                                AND fd.live_to < ? #live_from
                                AND live_to != '0000-00-00'
    	                    )

    bazz

  2. #2
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    groan: that errors out.

    execute failed: You can't specify target table 'file_sequencer' for update in FROM clause at Pages line 718.

  3. #3
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Code:
    DELETE file_sequencer
      FROM file_sequencer
    INNER
      JOIN file_dates
        ON file_dates.file_id = file_sequencer.file_id
       AND file_dates.live_to < ? #live_from
       AND file_dates.live_to <> '0000-00-00'
     WHERE file_sequencer.business_id = ?
       AND file_sequencer.content_category = ?
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  4. #4
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    oh... I seem to have got it. well, it seems to work.

    Is this correct/best?

    Code MySQL:
    delete file_sequencer.* from file_sequencer 
      inner join file_dates as fd
         on fd.file_id = file_sequencer.file_id
      where file_sequencer.business_id = ?
        and file_sequencer.content_category = ?
        and fd.live_to < ? #live_from
        and fd.live_to != '0000-00-00'
      	and fd.live_to < curdate()

    bazz

  5. #5
    SitePoint Guru
    Join Date
    Sep 2008
    Posts
    977
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok, I was close. should've refreshed my sitepoint page before posting.

    Thanks rudy.

  6. #6
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Get rid of the .*

  7. #7
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by transio View Post
    Get rid of the .*
    actually in mysql it's valid
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  8. #8
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    448
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by r937 View Post
    actually in mysql it's valid
    Interesting... valid, but not necessary, I assume?

  9. #9
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,251
    Mentioned
    59 Post(s)
    Tagged
    3 Thread(s)
    that's what the square brackets mean, optional --
    Quote Originally Posted by da manual
    DELETE [LOW_PRIORITY] [QUICK] [IGNORE]
    tbl_name[.*] [, tbl_name[.*] ...]
    FROM ...
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"


Bookmarks

Posting Permissions

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