0

这是我的初始查询:

SELECT bid_tag.*
FROM bid_tag join
     (select serial_number, count(*) as cnt
      from bid_tag where user_id = 0
      group by serial_number
     ) tsum
     on tsum.serial_number = bid_tag.serial_number and cnt > 1
order by bid_tag.serial_number
LIMIT 0, 21000;

现在从这些结果中,我需要选择所有位置tag_design = 0 AND tag_size = 0,然后DELETE从数据库中选择这些记录。

我只是不知道如何对初始查询的结果运行查询。

4

2 回答 2

2

只需替换SELECTDELETE,它将删除已选择的行。

DELETE bid_tag.*
FROM bid_tag join
     (select serial_number, count(*) as cnt
      from bid_tag where user_id = 0
      group by serial_number
     ) tsum
     on tsum.serial_number = bid_tag.serial_number and cnt > 1
WHERE tag_design = 0 AND tag_size = 0
order by bid_tag.serial_number
LIMIT 0, 21000;
于 2013-08-01T17:55:25.697 回答
0

EXISTS在 where 子句中使用一个术语:

DELETE
  FROM bid_tag btd
 WHERE EXISTS (
           SELECT 1
             FROM (
                     SELECT bid_tag.*
                       FROM bid_tag bts
                       JOIN (
                               SELECT serial_number, count(*) as cnt
                                 FROM bid_tag btj
                                WHERE btj.user_id = 0
                             GROUP BY btj.serial_number
                            ) tsum
                         ON (     tsum.serial_number = bts.serial_number
                              AND tsum.cnt > 1
                            )
                      WHERE bts.tag_design = 0
                        AND bts.tag_size = 0
                   ORDER BY bts.serial_number
                      LIMIT 0
                          , 21000
                  ) rs_base
            WHERE rs_base.id = btd.id   -- PK column
      )
      ;

术语中的子查询EXISTS可以进一步嵌套,以包含对原始结果集的另一个查询。只需确保始终选择要执行删除的表的主键。

请注意,您可能不想在删除操作中将自己限制在结果集中的一部分,因此请检查您是否需要限制前 21000 个结果 - 如果不需要,请删除“ORDER BY”和“LIMIT”子句.

于 2013-08-01T18:00:41.607 回答