2010-12-09 36 views
1

我想重新格式化MySql表格以便在网络节点映射程序中使用。原来的格式是:MySql查询获取相同表格字段中所有元素的组合

| ID | story | org | scribe | 

,我想所有组织名称拉进两个输出表是这样的:

| org1 | org2 | scribe | weight of connection | 

ORG1原始表ORG2都来自同一个领域,通过共享一个或多个抄写员而彼此相关。所有抄写员都有唯一的ID。当然,我不想重复输入。

CAN做到目前为止是把所有由做一个“%文本%”为组织搜索,然后排除组织从输出连接到任何一个组织在列表中,像这样的机构单位:

SELECT 'tabitha' as org1, 
org as org2, 
teller as scribe_id, 
count(teller) as weight 
FROM `stories` 
WHERE teller in 
(
(SELECT 
teller 
FROM `stories` 
WHERE org like '%tabitha%' 
group by teller) 
) 
and org not like '%tabitha%' 
group by teller, org 

所以我觉得有关于自连接或当可能的工作情况下,一些伎俩,但我还没有发现任何东西。

+0

感谢您的答案,工作,稍微调整一下。 – 2010-12-09 21:12:29

回答

0

我并不十分清楚你想要做什么,但也许这样?

select t1.org as org1, t2.org as org2, teller as scrib_id, count(teller) as weight 
from stories t1 join stories t2 where t1.teller=t2.teller and t1.org!=t2.org 
group by teller,t1.org 

这将执行T1和T2之间的连接上取款(包括相同的表),它排除了加入到自己的记录

我可能是遥远,但也许有些版本的加入语法可能有帮助。

+0

不错!我以前从来没有在连接中使用过两个条件,但必须记住它在此处起作用。我所做的加入最接近的一团是获得累积分布的公式。我会后的最终查询下面:选择 t1.org为ORG1, t2.org为ORG2, t1.teller为scrib_id, 计数(不同t1.story)的重量 从故事点t1加入故事T2哪里t1.teller = t2.teller and t1.org!= t2.org and t1.org not in('none','[swahili]','[]') and t2.org not in('none' ,'[swahili]','[]') group by t1.teller,t1.org 按重量排序desc,t1.org; – 2010-12-09 21:11:44

0

此查询工作。从给出的解决方案中调整只是它没有正确计算权重。

select t1.org as org1, 
     t2.org as org2, 
     t1.teller as scrib_id, 
     count(distinct t1.story) as weight 
     /* need to count the stories instead of the scribes now */  
from stories t1 join stories t2 
where t1.teller=t2.teller 
    and t1.org!=t2.org and t1.org not in ('none','[swahili]','[]') 
    /* this just excludes nonsense categories */ 
    and t2.org not in ('none','[swahili]','[]') 
group by t1.teller,t1.org 
order by weight desc, t1.org; 

对于我的下一个问题 - 我甚至不知道是否有可能,你可以问SQL上做柜员或划线近似匹配?如果这些ID是电话号码,并且有人忘记了其中一位数字,我仍然想将它们组合在一起。我认为这太难以mysql了 - 我需要python或其他东西。

相关问题