How to delete rows not in subquery?

Discussion in 'MySQL' started by ChrisPhp, Mar 8, 2008.

  1. #1
    I have the followin query:
    
    SELECT DISTINCT (ip), page_id FROM `temp`)
    
    Code (mysql):
    It shows the the unique hits for every page.
    Now I want to delete all the rows that are not in the result of the query above. How do I do that?
    I've tried multiple things including the following, but that didn't work:
    
    DELETE FROM temp WHERE ip NOT IN(SELECT DISTINCT (ip), page_id FROM `temp`)
    
    Code (mysql):

     
    ChrisPhp, Mar 8, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What does 'didn't work' mean? Did it give you an error? Or didn't it delete anything?
    If it gives you an error, please tell us which one.
    If it doesn't delete anything, that's perfectly normal. Look closely at your query. What is says is: delete from the table 'temp' the ip's that aren't present in the same table 'temp'.
    Since the subquery selects all ip's present in the table (there is no WHERE clause), the WHERE clause of the delete will never be true, and nothing will be deleted.
     
    CreativeClans, Mar 11, 2008 IP