문제

I would like to reformat a MySql table for use in a network node mapping program. The original format is:

| ID | story | org | scribe |

and I would like to pull all org names into two output tables like this:

| org1 | org2 | scribe | weight of connection |

org1 and org2 both come from the same field in the original table, and are related to each other by sharing one or more scribes. All scribes have unique IDs. And of course I don't want duplicate entries.

What I CAN do so far is pull all the orgs that are connected to any one org in the list by doing a '%text%' search for the org and then excluding that org from the output, like so:

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

So I feel like there's some trick about self-joins or case when that might work, but I haven't found anything yet.

도움이 되었습니까?

해결책

I'm not totally clear on what you are trying to do, but perhaps something like this?

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

This will perform a join between t1 and t2 (both the same table) on teller, it excludes the records that join to themselves

I could be way off, but perhaps some version of the join syntax may help.

다른 팁

This query worked. Only tweak from solution given was that it wasn't calculating weights correctly.

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;

For my next question - I don't even know if it possible, can you ask sql to do an APPROXIMATE match on teller or scribe? If these IDs are phone numbers and someone forgets one of the digits, I'd still like to group them together. I assume that's too hard to mysql - I would need python or something.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top