我正在尝试根据类别ID计算数据库中的数据数量,并且我编写了以下代码行:
public function getPodcastByCategoryId($catId){
$args = array(
'fields' => array(
'podcast.id',
'podcast.title',
'podcast.description',
'podcast.duration',
'podcast.audio',
'podcast.image',
'podcast.category',
'podcast.added_date',
'categories.title AS category_title',
'(SELECT users.full_name FROM users WHERE id = podcast.added_by) as author',
'(SELECT COUNT(category) FROM podcast WHERE category = podcast.category) as episodes'
),
'join' => "LEFT JOIN categories on podcast.category = categories.id",
'where' => array(
'category' => $catId
),
);
return $this->select($args);
}
但是,函数getPodcastByCategoryId($catId)
给出的情节= 3,这是表播客中存在的数据计数。
数据库中的数据:
我的预期情节在类别ID 2中为2,在类别ID 1中为1。
答案 0 :(得分:2)
对于剧集,您应该使用COUNT(*)
$args = array(
'fields' => array(
'podcast.id',
'podcast.title',
'podcast.description',
'podcast.duration',
'podcast.audio',
'podcast.image',
'podcast.category',
'podcast.added_date',
'categories.title AS category_title',
'(SELECT users.full_name FROM users WHERE id = podcast.added_by) as author',
'(SELECT COUNT(*) FROM podcast WHERE categories.category = podcast.category) as episodes'
),
'join' => "LEFT JOIN categories on podcast.category = categories.id",
'where' => array(
'category' => $catId
),
);