sql - Query three tables with inner join -
sql - Query three tables with inner join -
i'm working on sql database 3 tables.
pageids links ranks
i want query database page name , find links given page name, furthermore want links sorted score.
the pagename used query either exists pagea or pageb, , if pagea want pagebs pagename , pagebs pagescore - , vice versa.
i know have utilize inner joins, how should handle 'or' in inner bring together , how pageid in pageids, query other table , pagename in pageids.
this have tried, take quite time , not opposite pagename
select * pageids inner bring together links on links.pageaid = pageids.pageid or links.pagebid = pageids.pageid inner bring together ranks on ranks.pageid = links.pagebid pageids.pagename = @searchternm
pageids
get pageid pagename matching @searchterm
+--------+----------+ | pageid | pagename | +--------+----------+ | 1234 | namea | | 4321 | nameb | | 3321 | namec | +--------+----------+
links
if found pageid = pageaid, pagebid , pagename pagebid, else pageaid , pagename pageaid
+---------+---------+ | pageaid | pagebid | +---------+---------+ | 1234 | 3321 | | 4321 | 3321 | | 1234 | 4321 | +---------+---------+
ranks
if found pageid = pageaid, score pagebid, else score pageaid
+--------+-----------+ | pageid | pagescore | +--------+-----------+ | 1234 | 1 | | 3321 | 4 | | 4321 | 2 | +--------+-----------+
if input nameb output should be:
pagename, score nodec, 4 nodea, 1
thanks help.
if understand requirements correctly you'll want bring together pageids table twice, 1 time pageaids , 1 time pagebids.
then have 1 query pick pagea details pageb matches, union reversed query pageb details pagea matches.
select pagea.pagename, ranksa.score links inner bring together pageids [pagea] on links.pageaid = pagea.pageid inner bring together pageids [pageb] on links.pagebid = pageb.pageid inner bring together ranks [ranksa] on links.pageaid = ranksa.pageid pageb.pagename = @pagename union select pageb.pagename, ranksb.score links inner bring together pageids [pagea] on links.pageaid = pagea.pageid inner bring together pageids [pageb] on links.pagebid = pageb.pageid inner bring together ranks [ranksb] on links.pagebid = ranksb.pageid pagea.pagename = @pagename
sql inner-join
Comments
Post a Comment