谷歌搜索后,我发现 SQL_CALC_FOUND_ROWS
可以在应用 limit
之前得出表中可用的总行数。
但在数据库中搜索一些文本时,我有非常复杂的 php sql 查询,并且还想知道数据库中的总可用结果。
SELECT i.name 'title',i.add_time 'time',round(i.price) 'price',round(i.new_price) 'new_price',
s.store_address 'address', s.name 'name'
FROM store_items i,stores s
WHERE i.store_id = s.store_id
AND (i.name like '%samsung glaxy%' AND
i.name like '%samsung%' AND i.name like '%galaxy%'
OR i.name like '%samsung%' OR i.name like '%galaxy%')
ORDER BY
CASE WHEN i.name like '%%'
AND i.name like '%samsung%' AND i.name like '%galaxy%' THEN 1
WHEN i.name like '%samsung%' OR i.name like '%galaxy%' THEN 2
END,
i.price ASC
LIMIT 0,25"
如何通过匹配以下结果获得可用行的总数。
是否可以使用 SQL_CALC_FOUND_ROWS
sql 函数。或 PHP
中可用的其他方式。
最佳答案
我将在下面介绍。希望你能找到有用的东西。您的查询没有什么不同,只是更大。查看found_rows()
也许。或者像 xDerived
这样的派生表。
此外,Obligatory Reading Percona 文章。
create table n1
( thing int not null
);
insert n1(thing) values
(1),(3),(1),(1),(17),(1),(17),(11);
-- 8 rows inserted
select SQL_CALC_FOUND_ROWS thing from n1 where thing<12 order by thing limit 0,2;
select found_rows() as foundRows;
+-----------+
| foundRows |
+-----------+
| 6 |
+-----------+
select count(*) as theCount
from
( select thing from n1 where thing<12 order by thing limit 0,2
) xDerived;
+----------+
| theCount |
+----------+
| 2 |
+----------+
关于PHP SQL 如何在使用 CASE 和表 JOIN 时获取总行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38454416/