Reputation:
NOTE: This is not the same as this question, as I need to get data from two other records, not two fields from one one other record!
MySQL newb. I have two tables, and I want to get data from both of them so I have the following:
wp_bowling_fixtures
fixture_id | fixture_date | home_team_id | away_team_id
-----------+--------------+--------------+-------------
1 | 2017-12-12 | 1 | 2
2 | 2017-12-12 | 3 | 4
3 | 2017-12-12 | 5 | 6
4 | 2017-12-12 | 7 | 8
5 | 2017-12-12 | 9 | 10
wp_bowling_teams
team_id | name | division | archived
--------+--------+----------+---------
1 | Team A | 1 | 0
2 | Team B | 1 | 0
3 | Team C | 2 | 1
4 | Team D | 2 | 0
5 | Team E | 3 | 0
6 | Team F | 3 | 0
7 | Team G | 4 | 0
8 | Team H | 4 | 1
9 | Team I | 4 | 0
10 | Team J | 4 | 0
The result I want a SELECT query to produce:
fixture_id | fixture_date | home_team_id | home_team_name | home_team_archived | home_team_division | away_team_id | away_team_name | away_team_archived | away_team_division
-----------+--------------+--------------+----------------+--------------------+--------------------+--------------+----------------+--------------------+-------------------
1 | 2017-12-12 | 1 | Team A | 0 | 1 | 2 | Team B | 0 | 1
I also want it ordered by fixture_date DESC, home_team_division ASC, home_team_name ASC.
Hope that makes sense.
TIA,
Nick.
Upvotes: 0
Views: 77
Reputation:
SELECT f.fixture_id, f.fixture_date, h.team_id as home_team_id, h.name as home_team_name, h.archived as home_team_archived, h.division as home_team_division, a.team_id as away_team_id, a.name as away_team_name, a.archived as away_team_archived, a.division as away_team_division FROM wp_bowling_fixtures f, wp_bowling_teams h, wp_bowling_teams a where f.home_team_id = h.team_id and f.away_team_id = a.team_id order by f.fixture_date desc, h.division asc, h.name asc;
Works.
Upvotes: 1
Reputation: 87
select x.fixture_id, x.fixture_date, x.home_team_id, x.home_team_name, x.home_team_archived, x.home_team_division,
y.away_team_id, y.away_team_name, y.away_team_archived, y.away_team_division from
(select a.fixture_id, a.fixture_date, a.home_team_id, b.name home_team_name, b.archived home_team_archived, b.division home_team_division
from wp_bowling_fixtures a inner join wp_bowling_teams b on A.home_team_id = B.team_id) x
inner join
(select a.fixture_id, a.fixture_date, a.away_team_id, b.name away_team_name, b.archived away_team_archived, b.division away_team_division
from wp_bowling_fixtures a inner join wp_bowling_teams b on A.away_team_id = B.team_id) y on x.fixture_id = y.fixture_id
order by x.fixture_date desc, home_team_division asc, home_team_name asc;`enter code here`
Upvotes: 0
Reputation: 74740
Nothing in sql stops you joining a table twice but if you do so you must alias them .
You're looking for a query that uses
fixtures f
JOIN teams h ON f.home_team_id = h.team_id
JOIN teams a ON f.away_team_id = a.team_id
I gave the teams h
(for home) and a
(for away) aliases so I could tell them apart. Give it a go at filling it out
Upvotes: 0