sql to select distinct records that start with the letter A

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
dsick
Forum Commoner
Posts: 57
Joined: Fri Mar 27, 2009 3:34 pm

sql to select distinct records that start with the letter A

Post by dsick »

whats the query to select a distict record that starts with a certain letter... before i would do it like this, i would have a column in my table that says starting letter and for each thing i insert into the database i would have a form feild and ask for its starting letter

then i would have a list of letters like A, B, C, D, E, F, G, H, I... so on

each one would be linked... say A would be linked to page.php?letter=A

that way i could select everything where the starting letter = the $letter

and the $letter variable would be equal to $_GET['letter']

i realize there is prolly a distinct query that could do the same thing..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: sql to select distinct records that start with the letter A

Post by John Cartwright »

Perhaps something like..

Code: Select all

SELECT DISTINCT `word` 
FROM `dictionary`
WHERE SUBSTRING(`word`, 1) = 'A'
.. untested.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: sql to select distinct records that start with the letter A

Post by VladSun »

Use[sql]... WHERE `word` >= 'A' AND `word` < 'B'[/sql]

This way MySQL may use indexes.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: sql to select distinct records that start with the letter A

Post by Eran »

This would also work:
[sql]... WHERE `word` LIKE 'A%'[/sql]
Post Reply