【发布时间】:2021-07-22 21:22:21
【问题描述】:
我创建了两个表。一个episode 表和一个season 表,我想知道如何选择和显示每个季节的剧集计数或将它们保存在season.episodes_count 中这样做的正确方法或查询是什么?
try {
$stmt = $db->prepare("SELECT * FROM `season` WHERE season.show_id = '" . $show_id . "' ORDER BY season_number DESC");
// how to select episodes_count?
$stmt->execute();
$seasons = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
echo $e;
}
【问题讨论】:
-
从表
season中删除episodes_count。将season_id添加到表episode中。然后JOIN两个表都使用COUNT() -
警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDO 或MySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data。 Escaping is not enough!
-
@B001ᛦ 你的意思是左加入
episodeonseasonASepisodes_count? -
@B001ᛦ 当我在节目页面中没有
season_id并循环显示它们时,我如何按季节对剧集进行分组?