1

我想重新格式化 MySql 表以用于网络节点映射程序。原始格式为:

| ID | story | org | scribe |

我想将所有组织名称拉到两个输出表中,如下所示:

| org1 | org2 | scribe | weight of connection |

org1 和 org2 都来自原始表中的同一字段,并且通过共享一个或多个 scribes 相互关联。所有抄写员都有唯一的 ID。当然,我不想要重复的条目。

到目前为止,我可以做的是通过对组织进行“%text%”搜索,然后从输出中排除该组织,来拉出连接到列表中任何一个组织的所有组织,如下所示:

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

所以我觉得有一些关于自加入或案例的技巧可能会起作用,但我还没有找到任何东西。

4

2 回答 2

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 (都是同一张表)之间的连接,它排除了连接到自己的记录

我可能会走得更远,但也许某些版本的连接语法可能会有所帮助。

于 2010-12-09T15:05:55.563 回答
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 什么的。

于 2010-12-09T21:15:13.703 回答