1: 通过find方法

 

$res = findAll(...); (or findAllByAttributes)

$num = count($res)

 

2: 通过count方法

 

$res1 = countByAttributes(...);

$res2 = countBySql(...);

 

3: 通过query方法

 

 
  1. $sql = "SELECT COUNT(*) as clients FROM client_table";  
  2. $command = Yii::app()->db->createCommand($sql);  
  3. $results = $command->queryAll();  
  4. $numClients = (int)$results[0]["clients"];  


通过queryScalar可以让上面的语句更简单:

 

 

 
    1. $sql = "SELECT COUNT(*) FROM client_table";  
    2. $numClients = Yii::app()->db->createCommand($sql)->queryScalar();  

相关文章:

  • 2022-01-21
  • 2022-01-08
  • 2022-12-23
  • 2021-07-15
  • 2021-10-14
  • 2022-12-23
  • 2021-12-18
猜你喜欢
  • 2021-11-04
  • 2021-11-23
  • 2021-08-02
  • 2022-12-23
  • 2022-12-23
  • 2022-02-11
  • 2022-12-23
相关资源
相似解决方案