MySQL 选择出现在不同行上且列具有多个特定值的 ID
我试图从关联表中选择满足同一字段的两个或多个值的项目,听起来很混乱,让我解释一下。
+-----------------------+
| 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
WHERE 子句中的表达式针对连接结果集的单行起作用。这就是
WHERE Category_id = 201 AND Category_id = 202
不起作用的原因 - 因为它不能是单行上的两个值。因此,您需要某种方法将表中的两行连接到结果集的一行中。您可以通过自连接来做到这一点:
当您想要搜索三个、四个、五个或更多值时,此技术很难扩展,因为它需要N-1 连接以匹配 N 个值。
因此,另一种方法是使用 GROUP BY:
这两种技术都可以,并且在不同情况下效果更好。
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:
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:
Both techniques are okay, and work better in different circumstances.
使用 JOIN 或 GROUP BY:
Use either a JOIN, or a GROUP BY:
假设每个item_id、category_id都是唯一的,
将c = 2替换为c =(类别数)
Assuming each item_id, category_id is unique,
Replace c = 2 with c = (count of categories)
我现在没有时间提供确切的查询,但请尝试这样的操作:
也许条件错误,但您可以尝试这种方式。
I don't have the time to provide the exact query right now, but try something like this:
Maybe conditions are wrong but you can try something this way.
你可以尝试:(
或者,正如评论所说......你可能不会。)
You could try:
(or, as the comments say... you may not.)