Yii查询怎么返回结果的条数
1个回答
展开全部
1.如果你写的SQL语句比较简单,建议使用下面的方法:
//得到行数目或者其他数目 count
// get the number of rows satisfying the specified condition
$n=Post::model()->count($condition,$params);
// get the number of rows using the specified SQL statement
$n=Post::model()->countBySql($sql,$params);
// check if there is at least a row satisfying the specified condition
$exists=Post::model()->exists($condition,$params);
2.如果你写的SQL语句比较复杂,建议使用下面的方法:
$sql = "SELECT ...... FROM .........LEFT JOIN offer o ON o.`offer_id`=......";
$total_sql = "SELECT COUNT(o.offer_id) num ".strstr($sql, 'FROM');
$this->connection->createCommand($total_sql)->queryScalar();
//得到行数目或者其他数目 count
// get the number of rows satisfying the specified condition
$n=Post::model()->count($condition,$params);
// get the number of rows using the specified SQL statement
$n=Post::model()->countBySql($sql,$params);
// check if there is at least a row satisfying the specified condition
$exists=Post::model()->exists($condition,$params);
2.如果你写的SQL语句比较复杂,建议使用下面的方法:
$sql = "SELECT ...... FROM .........LEFT JOIN offer o ON o.`offer_id`=......";
$total_sql = "SELECT COUNT(o.offer_id) num ".strstr($sql, 'FROM');
$this->connection->createCommand($total_sql)->queryScalar();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询