Q:

mysql query to exclude duplicate records

I have a quick question. I have the following table in the database.
CREATE TABLE IF NOT EXISTS `wp_agp_settings_data` (
  `ID` int(50) NOT NULL AUTO_INCREMENT,
  `agp_user_id` int(10) NOT NULL,
  `agp_order_id` int(10) NOT NULL,
  `agp_blog_id` int(10) NOT NULL,
  `agp_keywords` varchar(255) NOT NULL,
  `agp_keywords_date` varchar(20) NOT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;

INSERT INTO `wp_agp_settings_data` (`ID`, `agp_user_id`, `agp_order_id`, `agp_blog_id`, `agp_keywords`, `agp_keywords_date`) VALUES
(1, 2, 5, 4, 'keyword1', '1369872000'),
(2, 2, 0, 6, 'keyword2', '1369872000'),
(3, 2, 0, 3, 'masterkey', '1369872000'),
(4, 2, 0, 4, 'testkey', '1369872000');

I would like to get only distinct agp_blog_ids.
So far I did this:
SELECT agp_blog_id
FROM wp_agp_settings_data
WHERE
        agp_user_id = 2
        AND (agp_keywords NOT LIKE "%keyword1%" OR agp_keywords NOT LIKE "%key1%")
        AND agp_keywords_date BETWEEN 1369872000 AND 1370044800
GROUP BY agp_blog_id

However I would like to exclude result "4" at all because one record already matched the conditions.
Some help will be appreciated.

A:

I think this is what you are trying to do:
SELECT agp_blog_id
FROM wp_agp_settings_data
WHERE
        agp_user_id = 2
        AND agp_blog_id NOT IN(
                SELECT agp_blog_id FROM wp_agp_settings_data WHERE agp_keywords LIKE "%keyword1%")
        AND agp_keywords_date BETWEEN 1369872000 AND 1370044800
GROUP BY agp_blog_id

This will exclude any results that have 'keyword1' in the agp_keywords column.  You can add additional keywords to be excluded inside the 'NOT IN' block

