0

I have a table named content with the following fields: id (key), title, alias, state and hits.

There are many duplicated records (different ids, but same title and same alias). The "state" field controls if a record is Published (1) or Unpublished (0).

At the moment I was able to unpublish duplicated records:

UPDATE content
SET content.state = 0
WHERE content.alias IN
(
  SELECT alias FROM
  (
    SELECT `alias`, COUNT(*) `tot`
    FROM `content`
    GROUP BY `alias`
    HAVING `tot` > 1
  ) AS tmptable
)

but this doesn't take into account which record has the greater "hits".

My goal is to unpublish duplicated records with smaller hits.

4

2 回答 2

1

我建议使用此查询:

UPDATE content
SET content.state = 0
WHERE content.alias IN
(
    SELECT DISTINCT c1.`alias`
    FROM `content` c1, `content` c2
    WHERE c1.`id` <> c2.`id`
    AND c1.`alias` = c2.`alias`
    AND c1.`hits` <= c2.`hits`
) AS tmptable

子查询将选择命中率较小的重复别名。

于 2012-09-12T15:07:45.930 回答
0
UPDATE content
SET content.state = 0
WHERE content.id NOT IN 
(
  SELECT DISTINCT a.id
  FROM content AS a
  LEFT JOIN content b ON b.alias = a.alias AND a.hits < b.hits
  WHERE b.id IS NULL
)

编辑:几乎和乔斯林写的一样。

于 2012-09-12T15:20:34.203 回答