我正在做一个简单的 MySQL 查询来计算查询返回的行数,而没有 LIMIT
子句的影响。我将 Active Records 与 Codeigniter PHP 框架结合使用。
问题:使用SQL_CALC_FOUND_ROWS
时出现错误。为什么会这样?
查询
SELECT `listing_id`, SQL_CALC_FOUND_ROWS listing_id FROM (`listings`) LIMIT 100
错误
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SQL_CALC_FOUND_ROWS listing_id FROM (
listings
) LIMIT 100' at line 1
Codeigniter 事件记录
$this->db->select('listing_id')
->select('SQL_CALC_FOUND_ROWS listing_id', FALSE)
->from('listings')
->where('price < 1000')
->limit($limit, $offset)
->order_by('listing_id', 'desc');
最佳答案
SQL_CALC_FOUND_ROWS
不返回值,它只是一个修饰符,指示应保存行数——其中未考虑 LIMIT
子句因此稍后可以通过使用第二个查询(无需两次生成完整的结果集)来检索它。可以将其视为 DISTINCT
关键字。
更多信息,请阅读documentation关于这个话题。
关于php - 使用 SQL_CALC_FOUND_ROWS() 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9343626/