[英]MySQL SELECT two records from other table?
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. MySQL 新手。 I have two tables, and I want to get data from both of them so I have the following:我有两个表,我想从它们中获取数据,所以我有以下内容:
wp_bowling_fixtures 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 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:我想要一个 SELECT 查询产生的结果:
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.我还希望它按fixture_date DESC、home_team_division ASC、home_team_name ASC 排序。
Hope that makes sense.希望这是有道理的。
TIA, TIA,
Nick.缺口。
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; 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 按 f.fixture_date desc, h.division asc, h.name 排序
Works.作品。
Nothing in sql stops you joining a table twice but if you do so you must alias them . sql 中的任何内容都不会阻止您加入一个表两次,但如果这样做,您必须为它们设置别名。
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.我给了球队h
(主场)和a
(客场)别名,这样我就可以区分他们了。 Give it a go at filling it out试一试填写
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`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.