【发布时间】:2018-06-02 22:38:53
【问题描述】:
我需要提取所有匹配到特定日期的国家,表格结构是这样的:
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 有一个包含所有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);
});
这将返回:
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"] . "");
【问题讨论】:
-
因为你在比赛台上而不是在圆桌上离开了join,非常糟糕的模型形成