简体   繁体   English

删除具有多个条件的记录

[英]Delete records with multiple conditions

I need to exclude duplicate records in the bidding_price column with the following conditions: 我需要在满足以下条件的情况下,在bidding_price列中排除重复的记录:

Table: bid_account 表格: bid_account

Columns to check: 要检查的列:

id = PRIMARY KEY id =主键
auction_id = ID of each product auction_id =每个产品的ID
bidding_price = inserted value (this must be checked for duplicity for each product) bidding_price =插入值(必须检查每个产品的重复性)
bid_flag = must always equal the value of: 'd' bid_flag =必须始终等于以下值: “ d”
bidding_type = must always equal the value of:: 's' bidding_type =必须始终等于以下值:: '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 ). 它将在bidding_price列中始终存在相等的记录,而不能包含具有相同产品ID( auction_id )的相等记录。

Example of how it should not have: 它不应该具有的示例:

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

The correct would be: 正确的是:

auction_id | auction_id | bidding_price bidding_price
------10------------0.02 ------ 10 ------------ 0.02
------11------------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) ID是表中的PRIMARY KEY ,因此您可以将MAX(id)作为您的预订ID,然后使用NOT IN来删除没有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 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 如果要获取最低值ID,可以在where子句的子查询中使用MIN(id)

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 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. not-in子句应为您提供所有不需要的记录。 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'; 因此,请从bid_account中删除ID不在的出价(通过#sub查询以获取所需的ID#),然后ba.bid_flag ='d'和ba.bidding_type ='s'; should delete the duplicate records. 应该删除重复的记录。

You can delete it by using your PRIMARY KEY: ID. 您可以使用您的PRIMARY KEY:ID将其删除。 It is used to uniquely identify the record for delete action. 它用于唯一标识要删除操作的记录。 See demo here: http://sqlfiddle.com/#!9/603d56/1 在此处查看演示: 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). 它利用在子查询中选择ID的方法(两次选择该ID将避免错误:无法指定目标表进行更新)。 The subquery is similar to your query and it selects the id that will be retained. 子查询与您的查询类似,它选择将保留的ID。 Using NOT IN means delete the rest of the rows not equal to these ids in the subquery. 使用NOT IN意味着删除子查询中不等于这些ID的其余行。

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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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