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
Is there a way to do this in MySQL?
EDIT What is the advantage of using SUBSTRING over LEFT and vice versa?
EDIT Currently there are about 1700 records in the table and growing.
Bare usage of DISTINCT will return the first occurrence. However, it can work either way by sorting the initial results first before conducting the distinction step.
To get unique or distinct values of a column in MySQL Table, use the following SQL Query. SELECT DISTINCT(column_name) FROM your_table_name; You can select distinct values for one or more columns. The column names has to be separated with comma.
The SQL SELECT DISTINCT StatementThe SELECT DISTINCT statement is used to return only distinct (different) values. Inside a table, a column often contains many duplicate values; and sometimes you only want to list the different (distinct) values.
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
Works like a charm.
Sounds simple:
select distinct substring(field,1,1) as char from mytable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With