CodeIgniter 获取表计数行
Posted
技术标签:
【中文标题】CodeIgniter 获取表计数行【英文标题】:CodeIgniter get table count rows 【发布时间】:2019-09-26 10:16:34 【问题描述】:我是 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)
我使用限制进行分页,但我想从表中获取所有记录。
【问题讨论】:
这是来自 codeigntor 中联合的正确查询吗? 【参考方案1】:您可以在 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
your_query = 您当前正在使用的完整实际查询。
虽然我不确定 Codeigniter。但肯定是 SQL。
【讨论】:
【参考方案2】:* 如果您计算所有数据的所有记录,包括限制,则可以使用此代码。请检查一下。我希望它对你有用。*
$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();
【讨论】:
以上是关于CodeIgniter 获取表计数行的主要内容,如果未能解决你的问题,请参考以下文章