PLSQL 中的自连接
我有一个表 P,其中包含 personid 列和 licensetypeid 列。
每个人都可以有多种许可证类型,为该人员 ID 添加额外的行。
我需要找到 personid 的许可证 ID 分别为 1 和 5 的行。
我无法编写:
SELECT personid, licensetypeid
FROM P
WHERE licensetypeid=1 AND licensetypeid=5;
我听说我应该使用自连接来执行此操作。我该如何进行自连接来解决这个问题?
I have a table, P, with a personid column and licensetypeid column.
Each person can have multiple license types, adding additional rows to the table for that person ID.
I need to find the rows where personid has both licenseid of 1 and of 5.
I can't write:
SELECT personid, licensetypeid
FROM P
WHERE licensetypeid=1 AND licensetypeid=5;
I heard I should use a self-join to do this. How do I do a self join to solve this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
自连接和其他多次访问表的技术可以工作,但可能会降低性能,并且如果您需要推广到更大的 id 集,则这些技术会很笨重。
您可以通过计算每个人的匹配行数来通过对表的单个引用来完成
此操作:如果您想要需要更大的许可证类型ID值,则可以轻松扩展:(
在自连接版本中,您必须添加额外的加入每个附加值)
或者如果您想在更大的类型集中查找至少具有 2 种类型的人:
现在,与示例查询不同,licensetypeid 不包含在结果集中。如果出于某种原因有必要这样做,您可以在有 2 个值的情况下执行一个简单的技巧:
但更通用的方法是将这些值分组到一个简单的集合中:
Self-joins and other techniques of accessing the table more than once will work, but may reduce performance and are unwieldy if you need to generalize to larger sets of ids.
You can do it with a single reference to the table by counting the number of matching rows per person:
This can easily be expanded if you want to require a larger licensetypeid values:
(in the self-join version you would have to add an additional join for each additional value)
Or if you want to find people that have at least 2 types out of a larger set of types:
Now, unlike your sample query, the licensetypeid is not included in the result set. If that is necessary for some reason, you can do a simple trick in the case of 2 values:
But a more general approach is to group the values into a simple collection:
试试这个
Try this
如果您所需的许可证类型集是“固定的”,或者至少如果所需许可证类型集的基数是固定的,则给定的答案将正常工作。
否则,您需要编写与所谓的“关系除法”等效的 SQL。
其过程如下:
(1) 计算缺少至少一种所需许可证类型的人员集合:
NEEDELICETYPE 表示在特定调用上计算所需许可证类型集合所需的任何 SQL 语句。
(2) 选择(1)中没有出现的号码的人员数据:
If your set of required license types is "fixed", or at least if the cardinality of the set of required license types is fixed, then the given answers will work OK.
Otherwise, you need to write the SQL equivalent of what is known as "relational division".
That goes as follows:
(1) Compute the set of persons that LACK at least one of the required license types :
NEEDELICENSETYPE represents whatever SQL statement you need to compute the set of required license types on a particular invocation.
(2) Select the data for the persons with a number that does not appear in (1) :