简体   繁体   中英

count rows from another join table

在此处输入图像描述

i have created two tables. one episode table and one season table and i wanna know how can i select and display episodes counts for each season or save them in season.episodes_count what is the right way or query for doing this?

在此处输入图像描述

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

Hi don't have your DB to test against and this is not tested but somehitng like this:

    $stmt = $db->prepare("
    SELECT se.id,
    se.season_number,
    COUNT(es.id) AS episodes_count
    FROM season AS se
    LEFT JOIN espisode AS es ON (se.id = es.season_id)
    WHERE se.show_id = $show_id
    ORDER BY se.season_number DESC
    ");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM