我写了这个查询,但是“分组依据”非常愚蠢……所以,我该如何纠正呢?
SELECT COUNT(*) AS total, 'x' as test FROM contents WHERE name LIKE 'C%' GROUP BY test ORDER BY id ASC
欢迎提供不同的解决方案和有关性能的信息(也许使用DISTINCT?)
提前致谢!
这应该和其他选项一样好-
SELECT LEFT(name, 1) AS first_letter, COUNT(*) AS total FROM contents GROUP BY first_letter
如果您想一次只对一个字母运行此查询,则可以添加WHERE子句并删除GROUP BY-
SELECT COUNT(*) AS total FROM contents WHERE name LIKE 'a%'