DELETE not SELECTed row PHP-MySQL

Discussion in 'PHP' started by dracula51, Nov 24, 2009.

  1. #1
    i want to delete all rows from a table EXCEPT top 10 (order by view_count)

    SELECT * FROM table ORDER BY view_count LIMIT 10

    so now i need a query that delete all other table which r not selected by above query


    tnx in advance :)
     
    dracula51, Nov 24, 2009 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    can you give the primary key column name?
    It should look something like
    DELETE FROM `table` WHERE `primary_key_name` NOT IN (SELECT `primary_key_name` FROM `table` LIMIT 10)
    Code (markup):
     
    JAY6390, Nov 24, 2009 IP
  3. dracula51

    dracula51 Peon

    Messages:
    146
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    there's no primary key actually
    only two fields.
    id name & view_count

    i think its all about `view_count`

    & that query is not working...showing error :(
     
    dracula51, Nov 24, 2009 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In that case you will need to create a primary key in the table with an auto increment value to distinguish each row. Otherwise you run the risk of it deleting rows you don't want it to
     
    JAY6390, Nov 24, 2009 IP