PHP/MySQL - 分析多个集合中的公共集合
假设我有两个表:people
和 families
。
families
有两个字段 - id
和 name
。 name
字段包含家族姓氏。
people
具有三个字段 - id
、family_id
和 name
- family_id
是该人所属家庭的 id。 name
字段是该人的名字。
这基本上是一种一对多的关系,一个家庭有很多人。
我想要获取一个名称集列表,按家族中最大名称集出现次数最多的顺序排序。
这可能没有多大意义......
为了进一步解释我想要什么,我们可以对每组名称进行评分。 “分数”是数组大小 * 跨族出现的次数。
例如,假设两个名字“John”和“Jane”都存在于三个家庭中 - 该组的“分数”将为 2*3 = 6。
我怎样才能获得一组名称的数组以及该组的“分数” ',按每组得分排序?
示例结果集(我将其放在表格布局中,但这可能是 PHP 中的多维数组) - 请注意,这只是随机想到的,并不反映任何统计名称数据。
names | occurrences | score
Ben, Lucy | 4 | 8
Jane, John | 3 | 6
James, Rosie, Jack | 2 | 6
Charlie, Jane | 2 | 4
只是为了澄清,我对以下集合不感兴趣:
- 出现次数为 1(显然,只有一个家庭)。
- 集合大小为1(只是一个通用名称)。
我希望我已经解释了我有些复杂的问题 - 如果有人需要澄清,请说。
Let's say I have two tables, people
and families
.
families
has two fields - id
and name
. The name
field contains the family surname.
people
has three fields - id
, family_id
and name
- The family_id
is the id of the family that that person belongs to. The name
field is that person's first name.
It's basically a one to many relationship with one family having many people.
I want to get a lists of name sets, ordered by the highest occurrence of the largest set of names across families.
That probably doesn't make much sense...
To explain what I want further, we can score each set of names. The 'score' is the array size * number of occurrences across families.
For example, let's say two names, 'John' and 'Jane' both existed in three families - That set's 'score' would be 2*3 = 6.
How could I get an array of sets of names, and the set's 'score', ordered by each set's score?
Sample Result Set (I've put it in a table layout, but this could be a multi-dimensional array in PHP) - Note this is just randomly thought up and doesn't reflect any statistical name data.
names | occurrences | score
Ben, Lucy | 4 | 8
Jane, John | 3 | 6
James, Rosie, Jack | 2 | 6
Charlie, Jane | 2 | 4
Just to clarify, I'm not interested in sets where:
- The number of occurrences is 1 (obviously, just one family).
- The set size is 1 (just a common name).
I hope I have explained my somewhat complex problem - if anyone needs clarification please say.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,明白了:
很抱歉,我刚刚意识到我使用了一个无法在此处获取的查询库,但是从注释中,您将可以轻松地创建数组,如“初始化”部分中所示。
基本上,我所做的就是从我保留当前名称列表中所有名称所属的家族数组的对开始,然后将其与所有尚未尝试的名称相交。
OK, got it:
I am sorry I just realized I use a query lib that I can't source in here, but from the comment you will easily be able to create the arrays as in the section "Initialize".
Basically what I do is starting with the pairs I keep an array of the families all the names in the current name list belong to, then intersect it with all not-yet tried names.
这行得通吗?
Will this work?