Sorting MySQL data into alphabetical order

Hello again,

If you read my previous question (above thred), I’d like to ask the same question again please, except this time for MySQL.

I have 5000 words stored in a MySQL table stored in a column called ‘words’, and I’d like to know how to pull records from that table according to their first letter. I.e. I’d like to pull all the records that start with the letter A together, all the B’s together, C’s, e.t.c.

I’d like to display all the records that begin with the letter A on one page, the B’s on another, then the C’s on another, and so on. But, never more than one letter on any one page. This is why the records need to be selected from the table and grouped according to their first letter.

All pages will have links for every letter of the alphabet so the users can jump to the letter (and therefore the page)they desire. Do you know how to achieve this please, in the most efficient way? The links aren’t a problem, only the grouping of records according to their letter of the alphabet

Basically, I’m trying to achieve the same end as normal numeric next page links and indexing, except with letters.

I hope this makes sense,

Thanks very much again, if you can help!

Regards,

Jason

Add “order by <fieldname>” to the end of your select statement.

Thanks Wayne,

But I’m aware of the ORDER BY statement already. How do I tell SQL to only select the A’s and not the other letters.

ORDER BY on its own will ORDER all the results alphabetically, insteaded of into grouped letter categories.

I hope this clarrifies my needs,

Thanks again,

Jason

select <fieldname> from <tablename> where <fieldname> like ‘A%’ order by Fieldname

You would need one of those for each letter. To automate it use CHR(var) to generate your letters.

i.e.


for ($i = 65; $i <= 91; $i++) {
   $sqlStr = "";
   $sqlStr .= "select <fieldname> from <tablename> where <fieldname> like \\'";
   $sqlStr .= Chr($i) . "\\' order by Fieldname";
<do stuff here...>
}

Thanks very much wayne!