Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    combining delete query

    is there a way where i can combine all of these into one query. I have multiple delete query and would rather have just one.


    Delete from x where challenger = bob AND challengee != smith
    Delete from x where challenger = smith
    Delete from x where challenger = bob AND challengee != smith
    Delete from x where challengee = bob

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    103
    Thanks
    0
    Thanked 14 Times in 14 Posts
    you can try using a temp table for example

    your #temp table contains following data
    challenger challengee
    bob smith
    smith blank
    bob blank

    delete x from x inner join #temp on x.challenger = #temp.challenger and
    x.challengee != #temp.challengee

    or

    delete x from x inner join #temp on x.challenger = #temp.challenger and
    x.challengee <> #temp.challengee


    delete x from x inner join #temp on x.challenger = #temp.challenger

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,155
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Code:
    Delete from x 
    where ( challenger = bob AND challengee != smith )
       or ( challenger = smith )
       or ( challenger = bob AND challengee != smith )
       or ( challengee = bob )
    But your 1st and 3rd conditions are identical. There's not need to duplicate. So
    Code:
    Delete from x 
    where ( challenger = bob AND challengee != smith )
       or ( challenger = smith )
       or ( challengee = bob )
    Which means we can then further reduce that to
    [code]
    Code:
    Delete from x 
    where ( challenger = bob AND challengee != smith )
       or ( challenger IN (smith, bob ) )
    Don't omit the parentheses or you will have a horrible mess.

    Jassi's solution would work, but it seems like 150% overkill for something this simple.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New to the CF scene
    Join Date
    Dec 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you Old Pedant!


  •  

    Posting Permissions

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