[英]Apply left join to extract data from multiple tables using their foreign key
[英]Cannot extract data with multiple LEFT JOIN
我需要提取匹配到特定日期的所有國家/地區,表格結構是這樣的:
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
如您所見,我在match
有爭議的地方有round_id
。 round
上的設計如下:
id: 65
season_id: 280
season
代表round
的容器,因此season
記錄具有以下結構:
id: 161
competition_id: 48
一個season
的competition
包含了所有的rounds
。 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);
});
這將返回:
SQLSTATE [42S22]:找不到列:1054“ on子句”中的未知列“ round.id”
更新:
$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"] . ""
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.