Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Joining 2 tables?

Author  Topic 

jarv
Posting Yak Master

131 Posts

Posted - 2014-02-03 : 06:32:13
Hi, in my database I have 2 tables I'd like to join.

Countries and Fixtures

Countries = Countryid, countryname

Fixtures = teama, teamb

I have a Countryid for teama and teamb and could like to display the countryname

this is what I tried:

SELECT *
FROM wc_fixtures
INNER JOIN wc_countries
ON wc_fixtures.countrya=wc_countries.countryid
AND wc_fixtures.countryb=wc_countries.countryid


nothing came back?!
Fixtures:


Countries:

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-03 : 06:42:45
you should join it twice

SELECT f.*,c1.country as countrya,c2.country as countryb
FROM wc_fixtures f
INNER JOIN wc_countries c1
ON f.countrya=c1.countryid
INNER JOIN wc_countries c2
ON f.countryb=c2.countryid





------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

jarv
Posting Yak Master

131 Posts

Posted - 2014-02-03 : 07:14:18
thank you so much!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-03 : 13:40:54
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -