如何按首字母计数行? [英] how to count rows by first letter?

查看:86
本文介绍了如何按首字母计数行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一个菜鸟问题!

a noob question here!

我写了这个查询,但是分组依据"非常愚蠢... 那么,我该如何纠正呢?

I wrote this query, but the "group by" is very stupid... so, how can I correct this?

SELECT
    COUNT(*) AS total,
    'x' as test
    FROM
contents
    WHERE name LIKE 'C%'
GROUP BY
    test
ORDER BY id ASC

欢迎提供不同的解决方案和有关性能的信息(也许使用DISTINCT吗?)

different solutions and info about performances are welcome ( maybe using DISTINCT? )

提前谢谢!

推荐答案

此选项应与其他选项一样执行-

This should perform as well as any other option -

SELECT
    LEFT(name, 1) AS first_letter,
    COUNT(*) AS total
FROM contents
GROUP BY first_letter

如果您想一次只对一个字母运行此查询,则可以添加WHERE子句并删除GROUP BY-

If you want to run this query for a single letter at a time you can add the WHERE clause and drop the GROUP BY -

SELECT COUNT(*) AS total
FROM contents
WHERE name LIKE 'a%'

这篇关于如何按首字母计数行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆