2010-12-09 35 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或其他東西。

相關問題