无法使用多个LEFT JOIN提取数据

时间:2018-06-02 12:48:06

标签: php sql slim

我需要提取所有匹配的国家/地区,表格结构为:

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表上有以下设计:

id: 65
season_id: 280

season代表round的容器,因此season记录具有以下结构:

id: 161
competition_id: 48

season的{​​{1}}包含所有competitionrounds记录包含competition

country

最后是id: 48 coutry_id: 2

country

如何提取所有今天比赛的国家?

这就是我的尝试:

id: 2
name: albania

这将返回:

  

SQLSTATE [42S22]:未找到列:1054'on clause'中的未知列'round.id'

更新:

$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);
});

1 个答案:

答案 0 :(得分:0)

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"] . ""