返回字段的DISTINCT第一个字符(MySQL) [英] Returning the DISTINCT first character of a field (MySQL)

查看:394
本文介绍了返回字段的DISTINCT第一个字符(MySQL)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成数据库中列的所有首字母的字符列表.下面的SQL表示我想返回的内容.

I would like to produce a character list of all of the first letters of column in my database. The SQL below illistrats what I would like to return.


SELECT DISTINCT first_character(name) FROM mydatabase

在MySQL中有没有办法做到这一点?

Is there a way to do this in MySQL?

编辑 相对于LEFT,使用SUBSTRING有什么优势,反之亦然?

EDIT What is the advantage of using SUBSTRING over LEFT and vice versa?

编辑 目前,该表中大约有1700条记录,并且还在不断增长.

EDIT Currently there are about 1700 records in the table and growing.

推荐答案

很抱歉,但是我确切地想出了我刚才要做的事情.

Sorry to do this, but I figured out exactly what I needed to do just now.


SELECT DISTINCT LEFT(name, 1) FROM mydatabase

这将返回该列中每一行开头的第一个不同的单个字符的列表.我添加了将其更改为以下内容,以字母数字顺序获取列表:

This returned a list of the first, distinct, single characters that each row in the column started with. I added changed it to the following to get it the list in alpha-numeric order:


SELECT DISTINCT LEFT(name, 1) as letter FROM mydatabase ORDER BY letter

像魅力一样工作.

这篇关于返回字段的DISTINCT第一个字符(MySQL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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