我的表有两列trial_id,category_id
我正在附加图像以使其更清晰。
我想要具有category_id 260,2880的trial_id记录。
好的,我可以使用“in”语句
运行查询某事“SELECT * FROM trial_category
WHERE category_id IN(260,2880)”
它将trial_id设为73,213,但73还有很多其他类别。所以这不应该显示在结果中。
结果应该只包含213个trial_id,或者我在213中添加另一个category_id,然后213不应该显示。
由于
答案 0 :(得分:1)
如果我说得对,你只需要只包含260或2880的trial_id(两者都是?)
SELECT *
FROM trial_category
WHERE category_id IN (260, 2880)
and trial_id NOT IN (select trial_id
from trial_category
where category_id NOT IN (260, 2880))
答案 1 :(得分:0)
试试这个:
SELECT
*
FROM
trial_category
WHERE
category_id IN ( 260, 2880 )
AND trial_id=213
这将只为这两个类别提供trial_id
:
SELECT
trial_id
FROM
trial_category
GROUP BY
trial_id
HAVING
GROUP_CONCAT( DISTINCT category_id ORDER BY category_id) = '260,2880'
答案 2 :(得分:0)
SELECT x.*
FROM trial_category x
LEFT
JOIN trial_category y
ON y.trial_id = x.trial_id
AND y.category_id NOT IN(260,2880)
WHERE x.category_id IN (260,2880)
AND y.id IS NULL;