0

假设我有两张桌子,people并且families.

families有两个字段 -idname。该name字段包含家庭姓氏。

people包含三个字段 -idfamily_id-namefamily_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(只是一个通用名称)。

我希望我已经解释了我有些复杂的问题 - 如果有人需要澄清,请说。

4

2 回答 2

1

好的,我知道了:

<?php
require_once('query.lib.php');

$db=new database(DB_TYPE,DB_HOST,DB_USER,DB_PASS,DB_MISC);
$qry=new query('set names utf8',$db);

//Base query, this filters out names that are in just one family
$sql='select name, cast(group_concat(family order by family) as char) as famlist, count(*) as num from people group by name having num>0 order by num desc';
$qry=new query($sql,$db);

//$qry->result is something like 
/*
Array
(
    [name] => Array
        (
            [0] => cathy
            [1] => george
            [2] => jack
            [3] => john
            [4] => jane
            [5] => winston
            [6] => peter
        )

    [famlist] => Array
        (
            [0] => 2,4,5,6,8
            [1] => 2,3,4,5,8
            [2] => 1,3,5,7,8
            [3] => 1,2,3,6,7
            [4] => 2,4,7,8
            [5] => 1,2,6,8
            [6] => 1,3,6
        )

    [num] => Array
        (
            [0] => 5
            [1] => 5
            [2] => 5
            [3] => 5
            [4] => 4
            [5] => 4
            [6] => 3
        )

)

$qry->rows=7
*/

//Initialize
$names=$qry->result['name'];
$rows=$qry->rows;
$lists=array();
for ($i=0;$i<$rows;$i++) $lists[$i]=explode(',',$qry->result['famlist'][$i]);

//Walk the list and populate pairs - this filters out pairs, that are specific to only one family
$tuples=array();
for ($i=0;$i<$rows;$i++) {
  for ($j=$i+1;$j<$rows;$j++) {
    $isec=array_intersect($lists[$i],$lists[$j]);
    if (sizeof($isec)>1) {
      //Every tuple consists of the name-list, the family list, the length and the latest used name 
      $tuples[]=array($names[$i].'/'.$names[$j],$isec,2,$j);
    }
  }
}

//Now walk the tuples again rolling forward, until there is nothing left to do
//We do not use a for loop just for style
$i=0;
while ($i<sizeof($tuples)) {
  $tuple=$tuples[$i];
  //Try to combine this tuple with all later names
  for ($j=$tuple[3]+1;$j<$rows;$j++) {
    $isec=array_intersect($tuple[1],$lists[$j]);
    if (sizeof($isec)>0) $tuples[]=array($tuple[0].'/'.$names[$j],$isec,$tuple[2]+1,$j);
  }
  $i++;
}

//We have all the tuples, now we just need to extract the info and prepare to sort - some dirty trick here!
$final=array();
while (sizeof($tuples)>0) {
  $tuple=array_pop($tuples);
  //name list is in $tuple[0]
  $list=$tuple[0];
  //count is sizeof($tuple[1])
  $count=sizeof($tuple[1]);
  //length is in $tuple[2]
  $final[]=$tuple[2]*$count."\t$count\t$list";
}

//Sorting and output is all that is left
rsort($final);
print_r($final);
?>

很抱歉,我刚刚意识到我使用了一个无法在此处获取的查询库,但从评论中您将能够轻松地创建数组,如“初始化”部分中所示。

基本上我所做的是从我保留当前名称列表中所有名称所属的家庭数组的对开始,然后将其与所有尚未尝试过的名称相交。

于 2011-12-23T02:05:14.773 回答
0

这行得通吗?

SELECT
    f.name AS 'surname',
    GROUP_CONCAT(DISTINCT p.name ORDER BY p.name) AS 'names',
    COUNT(DISTINCT p.name) AS 'distinct_names',
    COUNT(p.id) AS 'occurrences',
    COUNT(DISTINCT p.name) * COUNT(p.id) AS 'score'
FROM
    families f
    LEFT JOIN people p ON ( f.id = p.family_id )
GROUP BY
    f.id
ORDER BY
    f.name
于 2011-12-23T03:28:03.503 回答