Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 得到错误的选择_Mysql - Fatal编程技术网

Mysql 得到错误的选择

Mysql 得到错误的选择,mysql,Mysql,此代码未按预期工作。我想选择所有包含标签的帖子,这些标签一起或分别出现在(561562)中,但是没有任何标签,这些标签在(561562)中不存在 选择post\u id\u post 从post_有_标签 WHERE tags\u id\u tags IN(561562) 具有计数(标签\标识\标签) 这里演示- 此处演示-可能有点过火,但: SELECT post_id_post FROM post_has_tags WHERE tags_id_tags IN (561, 562) AND t

此代码未按预期工作。我想选择所有包含标签的帖子,这些标签一起或分别出现在(561562)中,但是没有任何标签,这些标签在(561562)中不存在

选择post\u id\u post
从post_有_标签
WHERE tags\u id\u tags IN(561562)
具有计数(标签\标识\标签)
这里演示-


此处演示-

可能有点过火,但:

SELECT post_id_post
FROM post_has_tags
WHERE tags_id_tags IN (561, 562)
AND tags_id_tags NOT IN (SELECT DISTINCT(tag_id) FROM tags WHERE tag NOT IN (561, 562));

(假设您有一个单独的标签表)

可能有点过分,但是:

SELECT post_id_post
FROM post_has_tags
WHERE tags_id_tags IN (561, 562)
AND tags_id_tags NOT IN (SELECT DISTINCT(tag_id) FROM tags WHERE tag NOT IN (561, 562));
(假设您有一个单独的标签表)

尝试以下操作:

select distinct p1.post_id_post from post_has_tags p1
where not exists (
  select * from post_has_tags p2
  where p1.post_id_post = p2.post_id_post and p2.tags_id_tags not in (561, 562)
)
我不想低于菲尔,所以我也加入了我的:)

试试这个:

select distinct p1.post_id_post from post_has_tags p1
where not exists (
  select * from post_has_tags p2
  where p1.post_id_post = p2.post_id_post and p2.tags_id_tags not in (561, 562)
)

我不想比菲尔差,所以我也加入了我的:)

如果你有一个更简洁的查询,我会给你一个+1。您不需要
WHERE。。在
中,对于我在回答中提到的外部查询,由于查询更简洁,您从我这里得到+1。您不需要
WHERE。。在
的外部查询中,就像我在回答中一样