Jump to content

1064 - You have an error in your SQL syntax (Deleting duplicates)


Recommended Posts

Datebase: blog

Table: nitendoposts

<?php

function clearDuplicatePosts(){	
global $wpdb;
$wpdb->query("
delete bad_rows.*
from $wpdb->nitendoposts as bad_rows
inner join (
select post_title, MIN(id) as min_id
from $wpdb->nitendoposts
group by post_title
having count(*) > 1
) as good_rows on good_rows.post_title = bad_rows.post_title
and good_rows.min_id <> bad_rows.id;
");
}

add_action('publish_post', 'clearDuplicatePosts');
?>

Does nothing. When I try to run it from phpMyAdmin using

 

DELETE bad_rows .  *  FROM nitendoposts AS bad_rows INNER JOIN (
SELECT post_title, MIN( id ) AS min_id
FROM nitendoposts
GROUP BY post_title
HAVING count( * ) >1
) AS good_rows ON good_rows.post_title = bad_rows.post_title
AND good_rows.min_id <> bad_rows.id

 

I get

 

#1064 - You have an error in your SQL syntax.  Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT post_title, MIN( id ) AS min_id

FROM nitendoposts

GROU

 

How do you get the script to remove duplicates by searching through the 'post_title' field?

<?php
/*
Plugin Name: Duplicate Posts Eraser

*/

function simpleDuplicatePosts(){    
    global $wpdb;
    $wpdb->query("
delete from posts
USING posts, posts as vtable
WHERE (posts.ID > vtable.ID)
AND (posts.post_title=vtable.post_title)
    ");
}

add_action('publish_post', 'simpleDuplicatePosts');
?>

 

works...if you don't have a table prefix.

 

From the config.php file....

 

$table_prefix  = 'WHATEVER';

 

How do you make this script check for a $table_prefix like WHATEVERposts?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.