要求:查詢文章內容既包含aa且包含cc的全部文章的文章標題sql
CREATE TABLE IF NOT EXISTS `article` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(100) NOT NULL, `content` varchar(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ; INSERT INTO `article` (`id`, `title`, `content`) VALUES (1, 'a', 'aa'), (2, 'a', 'cc'), (3, 'a', 'aa'), (4, 'c', 'cc'); 查詢SQL: SELECT kk.`title`,COUNT(1) times FROM ( SELECT `title`, `content` FROM `article` WHERE `content` IN ('aa', 'cc') GROUP BY `title`, `content` ) kk GROUP BY kk.`title` HAVING times>=2;
效率估計不好,請大牛指點點spa