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 5 of 5

Thread: delete/exists

  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts

    delete/exists

    The iner select finds duplicates by child_id
    Code:
    select * from tezaver_tree_adjacent  tta1 where exists
    (
    select child_id
    from tezaver_tree_adjacent tta
    where tta.CHILD_ID = tta1.CHILD_ID
    group by tta.CHILD_ID
    having count(tta.CHILD_ID)  > 1
    )
    The outer with exists finds all records that has to be deleted

    Code:
    delete tta1 from tezaver_tree_adjacent  tta1 ...
    --> Mysql error 1093 - Can't specify target table for update in FROM clause

    How to delete that ?
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,133
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Create a temp table, insert the problem child_id's into it, then do the update based on the temp table. Drop the temp table.
    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.

  • #3
    New Coder
    Join Date
    Oct 2013
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The outer with exists finds all records that has to be deleted
    Does it? Your query looks for values that occur more than once, and you then remove *all* records that have that value. You are not removing onlythe duplicates, you are removing the original value also.

  • #4
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,960
    Thanks
    120
    Thanked 76 Times in 76 Posts
    That is the purpose of it, remove all.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,133
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Here...this works:

    Code:
    mysql> create table tta ( id int, name varchar(20) );
    Query OK, 0 rows affected (0.05 sec)
    
    mysql> insert into tta values( 17, 'apple'),(18,'banana'),(17,'cherry');
    Query OK, 3 rows affected (0.00 sec)
    Records: 3  Duplicates: 0  Warnings: 0
    
    mysql> select * from tta;
    +------+--------+
    | id   | name   |
    +------+--------+
    |   17 | apple  |
    |   18 | banana |
    |   17 | cherry |
    +------+--------+
    3 rows in set (0.00 sec)
    
    mysql> delete tta FROM tta INNER JOIN (SELECT id, count(*) as cnt FROM tta GROUP BY id) AS X ON tta.id = X.id AND X.cnt > 1;
    Query OK, 2 rows affected (0.00 sec)
    
    mysql> select * from tta;
    +------+--------+
    | id   | name   |
    +------+--------+
    |   18 | banana |
    +------+--------+
    1 row in set (0.00 sec)
    Presumably you can see how to translate that into your actual table and field names.
    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.


  •  

    Posting Permissions

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