Una soluzione molto hacky è ottenere il thread_id
ordinato per view_count
in un GROUP_CONCAT
. Quindi, possiamo utilizzare le operazioni sulle stringhe per ottenere il thread_id
con minimo view_count
.
Nel tuo SELECT
clausola, invece di t.thread_id
, puoi provare quanto segue:
SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT t.thread_id
ORDER BY t.view_count ASC
SEPARATOR ','),
',',
1) AS thread_id_with_minimum_views
Ora, in base a SELECT
query per identificare i record duplicati con vista minima, DELETE
query per eliminare tali record da xf_thread
la tabella sarà la seguente:
DELETE t_delete FROM xf_thread AS t_delete
INNER JOIN (SELECT CAST(SUBSTRING_INDEX(GROUP_CONCAT(DISTINCT t.thread_id ORDER BY t.view_count ASC SEPARATOR ','), ',', 1) AS UNSIGNED) AS tid_min_view
FROM (SELECT * FROM xf_thread) t
INNER JOIN xf_post p ON p.thread_id = t.thread_id
WHERE t.first_post_id = p.post_id
AND t.user_id = 0
AND t.reply_count < 2
GROUP BY t.title, t.username, p.message
HAVING Count(t.title) > 1
AND Count(t.username) > 1
AND Count(p.message) > 1
ORDER BY t.thread_id) AS t_dup
ON t_delete.thread_id = t_dup.tid_min_view