Ask a Question related to MySQL, Design and Development.
-
dylan #1
sql join query
Hi,
I have a little problem that's driving me nuts, I'm sure there's a
simple solution that I'm overlooking.
The problem is this (I'm giving a simplified example):
I have two tables:
matches (match_id, team1_id, team2_id)
teams (team_id, team_name)
I need a query that shows:
team_id, team1, team2 (where team1 and team 2 are looked up from the
teams table)
I hope someone can help!
Thanks
dylan Guest
-
Inner Join Query
Hello Everyone, it's been ages since I posted here, but I just don't get table joins. The background, I am working with an Access db. There are... -
Add another join to a query
I need to add to the SELECT item "C.CATEGORY_ID" to the following query (another join?): Category_id is a column of table "blog_categories" which... -
INNER JOIN query question
I have 2 tables with the following columns (other data fields omitted for brevity): cases.caseid cases.uid profiles.uid profiles.locationid... -
SQL join query help
I have written a forum and am using the following query to search in it: $query="select topics.tid,f_messages.messid from f_messages left join... -
Query, Join on nearest
Hello, I am in a position where i need to create a query where i must join two tables on the nearest value. Imagine i have 2 tables, tbl_Trade... -
Paul Lautman #2
Re: sql join query
dylan wrote:
What you are saying doesn't make sense?> Hi,
>
> I have a little problem that's driving me nuts, I'm sure there's a
> simple solution that I'm overlooking.
>
>
> The problem is this (I'm giving a simplified example):
>
> I have two tables:
>
> matches (match_id, team1_id, team2_id)
> teams (team_id, team_name)
>
> I need a query that shows:
>
> team_id, team1, team2 (where team1 and team 2 are looked up from the
> teams table)
>
>
> I hope someone can help!
>
> Thanks
I assume that when you say you want "team_id, team1, team2" you mean team1 =
team1_name and team2 = team2_name?
But if that is the case, which team_id are you askinf for in the results,
that of team1 or that of team2?
If so this is a basic join as shown in the manual
SELECT
Paul Lautman Guest
-
dylan #3
Re: sql join query
team1 and team2 will have a different team_id in the matches table.
so basically, i want a query that says something like
Team_id team1_name team2_name
0 Team A Team B
1 Team C Team A
2 Team A Team C
where in the matches table the data is stored:
match_id team1_id team2_id
0 0 1
1 2 0
2 0 2
and in the teams table
team_id team_name
0 team A
1 team B
2 team C
Does that make any more sense?
Paul Lautman wrote:
> dylan wrote:> What you are saying doesn't make sense?> > Hi,
> >
> > I have a little problem that's driving me nuts, I'm sure there's a
> > simple solution that I'm overlooking.
> >
> >
> > The problem is this (I'm giving a simplified example):
> >
> > I have two tables:
> >
> > matches (match_id, team1_id, team2_id)
> > teams (team_id, team_name)
> >
> > I need a query that shows:
> >
> > team_id, team1, team2 (where team1 and team 2 are looked up from the
> > teams table)
> >
> >
> > I hope someone can help!
> >
> > Thanks
>
> I assume that when you say you want "team_id, team1, team2" you mean team1 =
> team1_name and team2 = team2_name?
>
> But if that is the case, which team_id are you askinf for in the results,
> that of team1 or that of team2?
>
> If so this is a basic join as shown in the manual
> SELECTdylan Guest
-
strawberry #4
Re: sql join query
dylan wrote:I don't understand it either. Did you mean:> team1 and team2 will have a different team_id in the matches table.
>
>
> so basically, i want a query that says something like
>
> Team_id team1_name team2_name
> 0 Team A Team B
> 1 Team C Team A
> 2 Team A Team C
>
>
> where in the matches table the data is stored:
>
>
> match_id team1_id team2_id
> 0 0 1
> 1 2 0
> 2 0 2
>
>
> and in the teams table
>
>
> team_id team_name
> 0 team A
> 1 team B
> 2 team C
>
>
>
>
> Does that make any more sense?
>
>
>
>
>
>
>
> Paul Lautman wrote:
>> > dylan wrote:> > What you are saying doesn't make sense?> > > Hi,
> > >
> > > I have a little problem that's driving me nuts, I'm sure there's a
> > > simple solution that I'm overlooking.
> > >
> > >
> > > The problem is this (I'm giving a simplified example):
> > >
> > > I have two tables:
> > >
> > > matches (match_id, team1_id, team2_id)
> > > teams (team_id, team_name)
> > >
> > > I need a query that shows:
> > >
> > > team_id, team1, team2 (where team1 and team 2 are looked up from the
> > > teams table)
> > >
> > >
> > > I hope someone can help!
> > >
> > > Thanks
> >
> > I assume that when you say you want "team_id, team1, team2" you mean team1 =
> > team1_name and team2 = team2_name?
> >
> > But if that is the case, which team_id are you askinf for in the results,
> > that of team1 or that of team2?
> >
> > If so this is a basic join as shown in the manual
> > SELECT
?> match_id team1_name team2_name
> 0 Team A Team B
> 1 Team C Team A
> 2 Team A Team C
strawberry Guest
-
-
strawberry #6
Re: sql join query
dylan wrote:Well in that case it's easy:> yeah, that's the result I want. Any ideas?
SELECT m1.match_id, t1.team_name AS team1_name,t2.team_name AS
team2_name
FROM matches AS m1
LEFT JOIN teams AS t1 ON m1.team1_id = t1.team_id
LEFT JOIN teams AS t2 ON m1.team2_id = t2.team_id
I added the 'AS's for clarity - but it'll work without them.
strawberry Guest
-
paul_lautman@yahoo.com #7
Re: sql join query
dylan wrote:When it comes down to it, computer programming is just a matter of> yeah, that's the result I want. Any ideas?
putting down a few words.
However it is IMPORTANT to put the CORRECT words in the CORRECT order.
What confused us all was that you repeatedly said that you wanted
team_id in the result set and yet you said that the team ids for both
teams were different. Even after I asked which of the 2 team ids you
wanted in the result set, you still replied that you wanted Team_id in
it.
If you'd put match_id in the required result set, you'd have got an
answer immediately.
paul_lautman@yahoo.com Guest
-
dylan #8
Re: sql join query
Hi,
Sorry, my mistake, but keep your hair on!
Anyway, thanks strawberry you've saved me a lot of stress.
dylan Guest
-
Captain Paralytic #9
Re: sql join query
dylan wrote:Too late for that, lost mine years ago!> Hi,
>
> Sorry, my mistake, but keep your hair on!
Captain Paralytic Guest



Reply With Quote

