Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Database > SQL Language
|
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 25th, 2009, 05:47 AM
Registered User
 
Join Date: May 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default SQL query for inner join 2 fld on 1 table

Hi

I have two tables tbl_team and tbl_match . Tbl_team stores the name of all the teams in a tournament and tbl_match store the date of match being played between two teams. Here is tables structure

tbl_team (team_id int, team_name text)

tbl_match (match id, team1_id, team2_id, match_date datetime)

Now i need to write a Sql query to display match_id , date and team names playing on that date

Issue here is how do i inner join a foriegn key twice coz team1_id and team2_id both relate to team_id in tbl_team

hope u r getting my point.

Below is sample SQL statments which doesnt work in this siuation .

Code:
select * from tbl_match inner join tbl_team on tbl_team.team_id=tbl_match.team_id1 inner join tbl_team on tbl_team.team_id=tbl_match.team_id2

Thanks 4 u time

Regards
suraj
 
Old March 25th, 2009, 08:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hello.

There is no problem in what you are trying to achieve. To make this work, you have to join two times the same table, giving differents names every time so the database can treat it like they where two differents tables.

Something like:

Code:
select * from tbl_match inner join tbl_team on tbl_team.team_id=tbl_match.team_id1 inner join tbl_team AS tbl_team_1 on tbl_team_1.team_id=tbl_match.team_id2
Something like that will do the trick.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
 
Old March 25th, 2009, 09:38 AM
Registered User
 
Join Date: May 2008
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thanks a lot @gbianchi

Thanks a lot @gbianchi

Your sql query worked like a magic.. i was stupid not to think that ways ..

thanks a lot ... once again


regards
Suraj jain





Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help using join in SQL query yogeshyl SQL Language 1 July 30th, 2008 04:12 AM
updating table w/ query that has left join msordo VB.NET 2002/2003 Basics 4 April 11th, 2008 11:26 AM
Update Query using inner join with a linked table ceekay Access 3 June 8th, 2007 12:35 PM
How can i use typed dataset with Join Sql query? vijaykumartaduri BOOK: Professional Crystal Reports for VS.NET 0 November 10th, 2005 04:02 AM
T-SQL Outer join query jaucourt SQL Server 2000 5 June 2nd, 2004 10:56 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.