SQL问题:排除记录
我有一个数据库(NexusDB(据说符合 SQL-92)),其中包含 Item 表、Category 表和多对多 ItemCategory 表,它只是一对键。 正如您所期望的,项目被分配到多个类别。
我希望所有最终用户选择
ItemID | 的所有项目 类别ID
--------------------------------
01 | 01
01 | 02
01 | 12
02 | 01
02 | 02
02 | 47
03 | 47 01
03 | 02
03 | 14
等等...
我希望能够选择分配给类别 X、Y 和 Z 但未分配给类别 P 和 Q 的所有 ItemID。
例如,对于上面的示例数据,假设我想要获取分配给类别 01 或 02 但不是 12 的所有项目(产生项目 02 和 03)。 类似于:
SELECT ItemID WHERE (CategoryID IN (01, 02))
...并从该集合中删除 SELECT ItemID WHERE NOT (CategoryID = 12)
这可能是一个非常基本的 SQL 问题,但它让我很难过片刻。 任何帮助w/b表示赞赏。
I have a database (NexusDB (supposedly SQL-92 compliant)) which contains and Item table, a Category table, and a many-to-many ItemCategory table, which is just a pair of keys. As you might expect, Items are assigned to multiple categories.
I am wanting to all the end user to select all items which are
ItemID | CategoryID
--------------------------------
01 | 01
01 | 02
01 | 12
02 | 01
02 | 02
02 | 47
03 | 01
03 | 02
03 | 14
etc...
I want to be able to select all ItemID's that are assigned to Categories X, Y, and Z but NOT assigned to Categories P and Q.
For the example data above, for instance, say I'd like to grab all Items assigned to Categories 01 or 02 but NOT 12 (yielding Items 02 and 03). Something along the lines of:
SELECT ItemID WHERE (CategoryID IN (01, 02))
...and remove from that set SELECT ItemID WHERE NOT (CategoryID = 12)
This is probably a pretty basic SQL question, but it's stumping me at the moment. Any help w/b appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以尝试使用除了
You could try with EXCEPT
我无法从 NexusDB 文档中确认 SELECT 他们支持子查询,但他们确实支持 LEFT OUTER JOIN 和 GROUP BY。 因此,这是一个在这些限制内有效的查询:
I can't confirm from the NexusDB documentation on SELECT that they support subqueries, but they do support LEFT OUTER JOIN and GROUP BY. So here's a query that works within these restrictions:
当然,您需要在 WHERE 子句中输入您想要获取的类别。
Of course you need to put in the WHERE clause what categories you want to get.
对于类别数量较少且已知的简单情况,您可以简单地使用多个联接来检查存在和不存在:
对于更一般的情况,给定匹配和不匹配的项目数量未知列表,您可以使用以下查询。 我为每个列表使用了一个表变量(在 SQL Server 中可用),但您可以根据需要对实际表或变量/参数列表使用选择。 这个想法仍然是一样的:
For the simple case that you have with a low and known number of categories you can simply use several joins to check for existence and non-existence:
For a more general case, given an unknown number of items in the match and don't match lists, you can use the following query. I've used a table variable (available in SQL Server) for each of the lists, but you can use a select against an actual table or a list of variables/parameters as needed. The idea remains the same: