Page 1 of 1

sql to select distinct records that start with the letter A

Posted: Sun Aug 30, 2009 9:36 pm
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..

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

Posted: Sun Aug 30, 2009 9:59 pm
by John Cartwright
Perhaps something like..

Code: Select all

SELECT DISTINCT `word` 
FROM `dictionary`
WHERE SUBSTRING(`word`, 1) = 'A'
.. untested.

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

Posted: Mon Aug 31, 2009 11:55 am
by VladSun
Use[sql]... WHERE `word` >= 'A' AND `word` < 'B'[/sql]

This way MySQL may use indexes.

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

Posted: Mon Aug 31, 2009 12:01 pm
by Eran
This would also work:
[sql]... WHERE `word` LIKE 'A%'[/sql]