MySQL 选择出现在不同行上且列具有多个特定值的 ID

发布于 2024-12-04 07:15:21 字数 919 浏览 2 评论 0原文

我试图从关联表中选择满足同一字段的两个或多个值的项目,听起来很混乱,让我解释一下。

+-----------------------+
| item_id | category_id |
+-----------------------+
|   1     |    200      |
|   1     |    201      |
|   1     |    202      |
|   2     |    201      |
|   2     |    202      |
|   3     |    202      |
|   3     |    203      |
|   4     |    201      |
|   4     |    207      |
+-----------------------+

在表中,我希望能够仅选择我通过的类别中的项目。例如,如果我传递类别 ID 201 和 202,我只想要两个类别中的项目(它们可以有其他类别,但至少需要属于我正在查询的类别),所以在这种情况下,我只想要第 1 项和第 2 项,因为它们是类别 201 和 202 中唯一的项。

我最初的 SQL 语句类似于

SELECT * FROM item_category WHERE category_id = 201 AND category_id = 202;

但显然这行不通。

SELECT * FROM item_category WHERE category_id = 201 OR category_id = 202;

上面的查询也不起作用,因为它也会返回第 4 项和第 3 项。

那么我该如何只选择至少必须属于这两个类别的项目呢?

请记住,我可能会传递 2 个以上的类别 ID。

感谢您的帮助。

I'm trying to select items from an associative table that have satisfy two or more values of the same field, sounds confusing, let me explain.

+-----------------------+
| item_id | category_id |
+-----------------------+
|   1     |    200      |
|   1     |    201      |
|   1     |    202      |
|   2     |    201      |
|   2     |    202      |
|   3     |    202      |
|   3     |    203      |
|   4     |    201      |
|   4     |    207      |
+-----------------------+

In the table, I want to be able to select only items which are in the categories that I pass. So for example, if I pass category IDs of 201 and 202, I would want only items that are in BOTH categories (they can have other categories but need to be in at least the categories I'm querying), so in this case, I would want items 1 and 2 only since they are the only ones that are in categories 201 and 202.

My initial SQL statement was something like

SELECT * FROM item_category WHERE category_id = 201 AND category_id = 202;

But obviously that won't work.

SELECT * FROM item_category WHERE category_id = 201 OR category_id = 202;

The above query also won't work because it'll return items 4 and 3 as well.

So how would I go about only selecting items that have to at least be in both categories?

Keep in mind that I might be passing more than 2 category IDs.

Thank you for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

稀香 2024-12-11 07:15:21

WHERE 子句中的表达式针对连接结果集的单行起作用。这就是 WHERE Category_id = 201 AND Category_id = 202 不起作用的原因 - 因为它不能是单行上的两个值。

因此,您需要某种方法将表中的两行连接到结果集的一行中。您可以通过自连接来做到这一点:

SELECT c1.item_id
FROM item_category AS c1
INNER JOIN item_category AS c2 ON c1.item_id = c2.item_id
WHERE c1.category_id = 201 AND c2.category_id = 202

当您想要搜索三个、四个、五个或更多值时,此技术很难扩展,因为它需要N-1 连接以匹配 N 个值。

因此,另一种方法是使用 GROUP BY:

SELECT c.item_id, COUNT(*) AS cat_count
FROM item_category AS c
WHERE c.category_id IN (201,202)
GROUP BY c.item_id
HAVING cat_count = 2

这两种技术都可以,并且在不同情况下效果更好。

Your expression in a WHERE clause works against a single row of the joined result set. That's why WHERE category_id = 201 AND category_id = 202 doesn't work -- because it can't be two values on a single row.

So you need some way to join two rows from the table into one row of the result set. You can do this with a self-join:

SELECT c1.item_id
FROM item_category AS c1
INNER JOIN item_category AS c2 ON c1.item_id = c2.item_id
WHERE c1.category_id = 201 AND c2.category_id = 202

This technique is hard to scale up when you want to search for three, four, five or more values, because it requires N-1 joins to match N values.

So another method is to use GROUP BY:

SELECT c.item_id, COUNT(*) AS cat_count
FROM item_category AS c
WHERE c.category_id IN (201,202)
GROUP BY c.item_id
HAVING cat_count = 2

Both techniques are okay, and work better in different circumstances.

熟人话多 2024-12-11 07:15:21

使用 JOIN 或 GROUP BY:

SELECT category_id, fieldhere, anotherfield
FROM item_category
WHERE category_id in (201,202)
GROUP BY category_id
HAVING count(category_id) = 2

Use either a JOIN, or a GROUP BY:

SELECT category_id, fieldhere, anotherfield
FROM item_category
WHERE category_id in (201,202)
GROUP BY category_id
HAVING count(category_id) = 2
不可一世的女人 2024-12-11 07:15:21

假设每个item_id、category_id都是唯一的,

select *, count(item_id) as c from item_category
where category_id in (201, 202)
group by item_id
having c = 2;

将c = 2替换为c =(类别数)

Assuming each item_id, category_id is unique,

select *, count(item_id) as c from item_category
where category_id in (201, 202)
group by item_id
having c = 2;

Replace c = 2 with c = (count of categories)

も星光 2024-12-11 07:15:21

我现在没有时间提供确切的查询,但请尝试这样的操作:

select item_category.* from 
item_category ,  item_category ic1, item_category ic2 where 
ic1.category_id = 201 and 
ic2.category_id = 202 and 
ic1.item_id = ic2.item_id and 
item_category.item_id = ic1.item_id and 
item_category.item_id = ic2.item_id;

也许条件错误,但您可以尝试这种方式。

I don't have the time to provide the exact query right now, but try something like this:

select item_category.* from 
item_category ,  item_category ic1, item_category ic2 where 
ic1.category_id = 201 and 
ic2.category_id = 202 and 
ic1.item_id = ic2.item_id and 
item_category.item_id = ic1.item_id and 
item_category.item_id = ic2.item_id;

Maybe conditions are wrong but you can try something this way.

爱格式化 2024-12-11 07:15:21

你可以尝试:(

SELECT * FROM item_category WHERE category_id IN (201, 202, ...)

或者,正如评论所说......你可能不会。)

You could try:

SELECT * FROM item_category WHERE category_id IN (201, 202, ...)

(or, as the comments say... you may not.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文