无法使用多个 LEFT JOIN 提取数据
Cannot extract data with multiple LEFT JOIN
我需要提取所有在特定日期进行比赛的国家/地区,table 结构是这样的:
TABLE NAME | FIELDS
country | id, name
round | id, season_id
season | id, competition_id
competition | id, country_id
match | id, round_id, date
单个 match
记录具有以下组成:
id: 60856
round_id: 65
date: 2018-06-02 00:00:00
如您所见,我在 round_id
中 match
存在争议。
在round
table上有如下设计:
id: 65
season_id: 280
season
表示round
的容器,所以season
记录有这样的结构:
id: 161
competition_id: 48
和一个 season
有一个包含所有 rounds
的 competition
。 competition
记录包含 country
:
id: 48
coutry_id: 2
最后 country
:
id: 2
name: albania
我如何提取所有今天进行比赛的国家/地区?
这是我试过的:
$app->get('/country/get_countries/{date}', function (Request $request, Response $response, array $args)
{
$sql = $this->db->query("SELECT * FROM country
LEFT JOIN `match` ON round_id = round.id
LEFT JOIN round ON season_id = season.id
LEFT JOIN season ON competition_id = competition.id
WHERE match.datetime = " . $args["date"] . "");
$sql->execute();
$countries = $sql->fetchAll();
return $response->withJson($countries);
});
这将 return:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'round.id' in 'on clause'
更新:
$sql = $this->db->query("SELECT * FROM country
LEFT JOIN competition ON country_id = competition.country_id
LEFT JOIN competition_seasons ON competition_id = competition.id
LEFT JOIN competition_rounds ON competition_rounds.season_id = competition_seasons.id
LEFT JOIN `match` ON match.round_id = competition_rounds.id
WHERE match.datetime = " . $args["date"] . "");
SELECT * FROM country
LEFT JOIN competition ON country.id = competition.country_id
LEFT JOIN season ON season.competition_id= competition.id
LEFT JOIN round ON round.season_id= season.id
LEFT JOIN match ON match.round_id= round.id
WHERE match.datetime = " . $args["date"] . ""
我需要提取所有在特定日期进行比赛的国家/地区,table 结构是这样的:
TABLE NAME | FIELDS
country | id, name
round | id, season_id
season | id, competition_id
competition | id, country_id
match | id, round_id, date
单个 match
记录具有以下组成:
id: 60856
round_id: 65
date: 2018-06-02 00:00:00
如您所见,我在 round_id
中 match
存在争议。
在round
table上有如下设计:
id: 65
season_id: 280
season
表示round
的容器,所以season
记录有这样的结构:
id: 161
competition_id: 48
和一个 season
有一个包含所有 rounds
的 competition
。 competition
记录包含 country
:
id: 48
coutry_id: 2
最后 country
:
id: 2
name: albania
我如何提取所有今天进行比赛的国家/地区?
这是我试过的:
$app->get('/country/get_countries/{date}', function (Request $request, Response $response, array $args)
{
$sql = $this->db->query("SELECT * FROM country
LEFT JOIN `match` ON round_id = round.id
LEFT JOIN round ON season_id = season.id
LEFT JOIN season ON competition_id = competition.id
WHERE match.datetime = " . $args["date"] . "");
$sql->execute();
$countries = $sql->fetchAll();
return $response->withJson($countries);
});
这将 return:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'round.id' in 'on clause'
更新:
$sql = $this->db->query("SELECT * FROM country
LEFT JOIN competition ON country_id = competition.country_id
LEFT JOIN competition_seasons ON competition_id = competition.id
LEFT JOIN competition_rounds ON competition_rounds.season_id = competition_seasons.id
LEFT JOIN `match` ON match.round_id = competition_rounds.id
WHERE match.datetime = " . $args["date"] . "");
SELECT * FROM country
LEFT JOIN competition ON country.id = competition.country_id
LEFT JOIN season ON season.competition_id= competition.id
LEFT JOIN round ON round.season_id= season.id
LEFT JOIN match ON match.round_id= round.id
WHERE match.datetime = " . $args["date"] . ""