I'm having trouble modifying this to delete the duplicate records. In this case, I had three posts with the same meta_value for the given meta_key and it returned two results (I may have a problem with the GROUP BY--still working it out). However changing the first SELECT to DELETE does not work. Isn't this the part I would change so data is deleted from both tables?
$sql1 = "SELECT *
FROM (
SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_type, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts, wp_postmeta
WHERE wp_posts.ID=wp_postmeta.post_ID
AND post_type='banners'
AND post_status='publish'
AND meta_key='uselink'
AND meta_value='http://www.mylink.com/subpage/')
AS a
INNER JOIN (
SELECT meta_value, MIN( post_id )
AS min_id
FROM wp_postmeta
GROUP BY meta_value
HAVING COUNT( * ) > 1 )
AS b
ON b.meta_value = a.meta_value
AND b.min_id <> a.id";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
while($row = $result1->fetch_assoc()) {
echo $row["meta_value"] . ", ";
}
} else {
echo "0 results";
}
This is what I tried (not working):
$sql1 = "DELETE *
FROM (
SELECT wp_posts.ID, wp_posts.post_title, wp_posts.post_type, wp_posts.post_status, wp_postmeta.meta_value
FROM wp_posts, wp_postmeta
WHERE wp_posts.ID=wp_postmeta.post_ID
AND post_type='banners'
AND post_status='publish'
AND meta_key='uselink'
AND meta_value='http://www.mylink.com/subpage/')
AS a
INNER JOIN (
SELECT meta_value, MIN( post_id )
AS min_id FROM wp_postmeta
GROUP BY meta_value HAVING COUNT( * ) > 1 )
AS b
ON b.meta_value = a.meta_value
AND b.min_id <> a.id";
I'm going off this one without a second joined table that works:
DELETE a.*
FROM wp_posts
AS a
INNER JOIN (
SELECT post_title, MIN( id )
AS min_id
FROM wp_posts
WHERE post_type = 'banners'
AND post_status = 'publish'
GROUP BY post_title HAVING COUNT( * ) > 1 )
AS b
ON b.post_title = a.post_title AND b.min_id <> a.id
AND a.post_type = 'banners'
AND a.post_status = 'publish'