mysql – 如何在单个列上执行AND操作?
作者:互联网
我有一个有两列的桌子;
CREATE TABLE IF NOT EXISTS `QUESTION_CATEGORY_RELATION` (
`question_id` int(16) NOT NULL,
`category_id` int(16) NOT NULL,
KEY `category_id` (`category_id`),
KEY `question_id` (`question_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `QUESTION_CATEGORY_RELATION` (`question_id`, `category_id`) VALUES
(1, 2),
(1, 3),
(2, 1),
(2, 2);
我想创建一个查询,搜索带有content_id 2和content_id 3的question_id
例如.:
SELECT *
FROM `QUESTION_CONTENTS_REL`
WHERE `content_id` = 2 AND `content_id` = 3
解决方法:
SELECT question_id
FROM QUESTION_CONTENTS_REL
WHERE content_id in (2, 3)
group by question_id
having count(distinct content_id) = 2
标签:sql,mysql,database,mysqli,boolean-logic 来源: https://codeday.me/bug/20190901/1784764.html