Take the 2-minute tour ×

how can I insert "on duplicate key" in this Code to remove duplicate words? or is there a better method that you know? thank you!!

this is my code:

function sm_list_recent_searches($before = '', $after = '', $count = 20) {
// List the most recent successful searches.
    global $wpdb, $table_prefix;
    $count = intval($count);
    $results = $wpdb->get_results(
        "SELECT `terms`, `datetime`
        FROM `{$table_prefix}searchmeter_recent`
        WHERE 3 < `hits` AND CHAR_LENGTH(`terms`) > 4
        ORDER BY `datetime` DESC
        LIMIT $count");
    if (count($results)) {

        foreach ($results as $result) {
            echo '<a href="'. get_settings('home') . '/search/' . urlencode($result->terms) . '">'. htmlspecialchars($result->terms) .'</a>'.", ";

share|improve this question

2 Answers 2

up vote 3 down vote accepted

ON DUPLICATE KEY UPDATE is used to cause an insert to update records if they already exist. It's not used for SELECT queries

What you want is to use the GROUP BY clause on your terms column to collapse it to unique values. You can use the MAX aggregate function to get the date of the latest record for those terms.

SELECT `terms`, MAX(`datetime`)
FROM `{$table_prefix}searchmeter_recent`
GROUP BY terms
ORDER BY `datetime` DESC
LIMIT $count

Make sure you have an index on the terms column, otherwise this will be a very expensive operation, especially if you're using it to make an autocomplete. You might even consider grouping your terms at insert time for best performance.

share|improve this answer
thank you!! great!! –  ali Mar 16 '10 at 15:05

Have you looked into using the distinct keyword in your query? This shows what it does: http://newsourcemedia.com/blog/mysql-distinct-to-remove-duplicate-listing/

share|improve this answer
DISTINCT will affect all columns, so terms with different datetime values will still be repeated. What you want is GROUP BY. –  Alex J Mar 16 '10 at 15:00

Your Answer


