gpt4 book ai didi

mysql - 如何对单个列执行 AND 运算?

转载 作者:可可西里 更新时间:2023-11-01 07:59:29 29 4
gpt4 key购买 nike

我有一个包含两列的表格;

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);

enter image description here

我想创建一个查询来搜索 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

关于mysql - 如何对单个列执行 AND 运算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13067472/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com