I've got the following php code:
$SQL = "select ID from mydb.Domains";$result = mysql_query($SQL);while(($row=mysql_fetch_assoc($r esult))) { $DomainID = $row['ID']; // Update this domain's incoming link count. $SQL = "update mydb.Domains set LinksIn=(select count(ID) from mydb.Links2 where GoingToDomainID=$DomainID), LinksOut=(select count(ID) from mydb.Links2 where ComingFromDomainID=$DomainID) where ID=$DomainID"; mysql_query($SQL);}
I'm wondering if there is a way to perform this in a single query. Playing with the sql a bit I did manage to come up with a select that gets the information:
select Domains.ID, (select count(ID) from mydb.Links2 where GoingToDomainID=Domains.ID) as LinksIn, (select count(ID) from mydb.Links2 where ComingFromDomainID=Domains.ID) as LinksOut from mydb.Domains, mydb.Links2 group by Domains.ID
But I'm not sure where to go from there.
Any advice would be appreciated. Thanks!
G-Man
$SQL = "select ID from mydb.Domains";$result = mysql_query($SQL);while(($row=mysql_fetch_assoc($r esult))) { $DomainID = $row['ID']; // Update this domain's incoming link count. $SQL = "update mydb.Domains set LinksIn=(select count(ID) from mydb.Links2 where GoingToDomainID=$DomainID), LinksOut=(select count(ID) from mydb.Links2 where ComingFromDomainID=$DomainID) where ID=$DomainID"; mysql_query($SQL);}
I'm wondering if there is a way to perform this in a single query. Playing with the sql a bit I did manage to come up with a select that gets the information:
select Domains.ID, (select count(ID) from mydb.Links2 where GoingToDomainID=Domains.ID) as LinksIn, (select count(ID) from mydb.Links2 where ComingFromDomainID=Domains.ID) as LinksOut from mydb.Domains, mydb.Links2 group by Domains.ID
But I'm not sure where to go from there.
Any advice would be appreciated. Thanks!
G-Man
Comment