CodeIgniter获取表计数行

时间:2019-09-26 10:16:34

标签: php sql codeigniter

我是CodeIgniter的新手,我想对数据库表中的所有行进行计数,但是我在查询中使用限制,我希望所有计数都没有使用限制,我该怎么办?

我的代码如下:

  $sql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
  FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
          UNION 
         SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM  
      " . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC 
     LIMIT $start, $per_page  ";

    $query = $this->db->query($sql)

我使用分页限制,但我想从表中获取所有记录。

2 个答案:

答案 0 :(得分:0)

您可以在UNION查询的上方和下方添加新列。如下所示。

select (select count(*) from your_query), your_columns from query_above_union 
UNION 
select (select count(*) from your_query), your_columns from query_below_union

您的查询=您当前正在使用的完整实际查询。

尽管我不确定Codeigniter。但是请确定SQl。

答案 1 :(得分:0)

*如果您用所有数据(包括限制)计数所有记录,则可以使用此代码。请检查一下。我希望它对您有用。*

$countsql = " SELECT intGlCode,fkCategoryGlCode,'C' as acctyp,varEmail,varContactNo as phone,CONCAT(varFirstName,' ',varLastName) as name,dtCreateDate,chrStatus,varMessage as message
 FROM " . DB_PREFIX . "Customer WHERE varEmail='$userEmail'
      UNION 
     SELECT intGlCode,'' as fkCategoryGlCode,'P' as acctyp,varEmail,varPhoneNo as phone,varName as name,dtCreateDate,chrStatus,txtDescription as message FROM  
  " . DB_PREFIX . "Power WHERE varEmail='$userEmail' ORDER BY intGlCode DESC";
$sql = $countsql. " LIMIT $start, $per_page";  
$totalRecords   = $this->db->query($countsql);
$result["total_rows"] = $totalRecords->num_rows();
$query = $this->db->query($sql);
$result["list"] = $query->result_array();