简体   繁体   中英

Delete records with multiple conditions

I need to exclude duplicate records in the bidding_price column with the following conditions:

Table: bid_account

Columns to check:

id = PRIMARY KEY
auction_id = ID of each product
bidding_price = inserted value (this must be checked for duplicity for each product)
bid_flag = must always equal the value of: 'd'
bidding_type = must always equal the value of:: 's'

It will always exist equal records in the bidding_price column, which it can not have is equal records with the same product ID ( auction_id ).

Example of how it should not have:

auction_id | bidding_price
------10------------0.02
------10------------0.02
------11------------0.02
------11------------0.02

The correct would be:

auction_id | bidding_price
------10------------0.02
------11------------0.02

I tried with the following command:

DELETE ba
    FROM bid_account ba JOIN
         (SELECT ba2.auction_id, ba2.bidding_price, MAX(ba2.id) as max_id
          FROM bid_account ba2
          WHERE ba2.bid_flag = 'd' AND ba2.bidding_type = 's'
          GROUP BY ba2.auction_id, ba2.bidding_price
         ) ba2
         ON ba2.auction_id = ba.auction_id AND
            ba2.bidding_price = ba.bidding_price AND
            ba2.max_id < ba.id
WHERE ba.bid_flag = 'd' AND ba.bidding_type = 's' AND ba.auction_id = ba2.auction_id

The problem is that it deleted multiple records that it should not delete, did not do the validations correctly. How can I do it?

ID is your PRIMARY KEY in the table, so you can get MAX(id) to be your Reservation ID,then use NOT IN to delete by ID without MAX(id)

You can try this.

DELETE ba FROM bid_account ba
WHERE ba.id NOT IN
(
  SELECT max_id FROM 
    (
      SELECT auction_id, bidding_price, MAX(id) max_id
      FROM bid_account 
      WHERE bid_flag = 'd' AND bidding_type = 's'
      GROUP BY auction_id, bidding_price
    ) t
)

sqlfiddle: http://sqlfiddle.com/#!9/0f2e5/1

EDIT

If you want to get lowest-value ID you could use MIN(id) in the subquery in the where clause

DELETE ba FROM bid_account ba
WHERE ba.id NOT IN
(
  SELECT min_id FROM 
    (
      SELECT auction_id, bidding_price, MIN(id) min_id
      FROM bid_account 
      WHERE bid_flag = 'd' AND bidding_type = 's'
      GROUP BY auction_id, bidding_price
    ) t
)

sqlfiddle: http://sqlfiddle.com/#!9/ffe92/1

The below statement should give you all the records that you need.

SELECT ba2.auction_id, ba2.bidding_price, MAX(ba2.id) as max_id
FROM bid_account ba2
WHERE ba2.bid_flag = 'd' AND ba2.bidding_type = 's'
GROUP BY ba2.auction_id, ba2.bidding_price;

A not-in clause should give you all the records that you don't need. So, delete from bid_account where id not in (#sub query to fetch required ids#) and ba.bid_flag = 'd' AND ba.bidding_type = 's'; should delete the duplicate records.

You can delete it by using your PRIMARY KEY: ID. It is used to uniquely identify the record for delete action. See demo here: http://sqlfiddle.com/#!9/603d56/1

It makes use of selecting the ID within a subquery(selecting it twice will AVOID the error: target table cannot be specified for update). The subquery is similar to your query and it selects the id that will be retained. Using NOT IN means delete the rest of the rows not equal to these ids in the subquery.

delete e.*
from bid_account e
where e.id not in (
select id from (
    select a.id
    from bid_account a
    join bid_account b
    on a.auction_id=b.auction_id
    and a.bidding_price=b.bidding_price
    and a.bid_flag=b.bid_flag
    and a.bidding_type=b.bidding_type
    where a.bid_flag='d' and a.bidding_type='s'
    and a.id < b.id) tt);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM