我的分类网站有九个类别。当用户搜索关键字时,我想显示搜索结果以及每个类别中的搜索结果数量。
如何优化sql查询?
我尝试了什么?为每个类别运行一个循环:
select * from ads where title like '%keyword%';
select count(*) from ads where title like '%keyword%' and category_id = 1;
select count(*) from ads where title like '%keyword%' and category_id = 2;
select count(*) from ads where title like '%keyword%' and category_id = 3;
select count(*) from ads where title like '%keyword%' and category_id = 4;
.....
有什么更好的建议可以使 sql 查询更快吗?