gpt4 book ai didi

mysql - 选择与相关表中的多行匹配的行

转载 作者:行者123 更新时间:2023-11-29 02:32:39 25 4
gpt4 key购买 nike

我有三个表:

项目

id   name   etc
--------------------
1 Rex
2 Fido
3 Geoff

类别

id   name
------------
1 Dogs
2 Humans
3 Mammals

类别项目

category_id  item_id
--------------------
1 1
3 1
1 2
3 2
2 3
3 3

我还有一组类别 ID。我想计算与数组中所有类别相关的项目数。

例如……

Category_ids    Result
----------------------
1,2 0
2,3 1
1,2,3 0

当我弄清楚这个问题时,我敢肯定我会踢自己一脚。

最佳答案

请尝试下面给出的查询..

select count(*) AS Result from (
SELECT count(item_id) FROM category_item
WHERE
category_id in (2 ,3)
GROUP by item_id

HAVING count(*) = 2
) AS temp

在此查询中,输入等于 category_id 总数的 count(*) 值,例如,如果您要检查 category_ids 1,2,3,则输入 having count(*) = 3。在此查询中,假设您提供 category_id 1 和 2,然后它将使用

获取 item_id 的存在总数

希望本次查询对您有所帮助。

谢谢

关于mysql - 选择与相关表中的多行匹配的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10633574/

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