I would think the easiest way would be to use the string functions of MySQL
itself...then you don't have the overhead of the PHP application having to
check each row of data (a wise person on this board once answered a question
similar to this for me).

Somthing like...

$query = "SELECT * FROM tbl_name WHERE LEFT(last_name, 1) == 'A')";

Using a variable passed in...

$query = "SELECT * FROM tbl_name WHERE LEFT(last_name, 1) == '$letter')";

NOTE: LEFT() is a "special" function, I'd consider it a derivative of
SUBSTRING()




-----Original Message-----
From: michael kimsal [mailto:[EMAIL PROTECTED]]
Sent: Sunday, March 10, 2002 8:17 PM
To: Chuck \ Pup\\ Payne
Cc: mysql lists.mysql.com
Subject: Re: [PHP] Re: A stupid question...


Chuck "Pup" Payne wrote:
> I want to sort by a letter in a colomn. Let say I want to sort the colomn
> last_name. I can do order by but I can do just the A's.
>
> http://www.myserver.com/mysort.php?Letter=A
>
> Like to create a link on a web "A" then sort only the last name are A.
>
> I hope that's helps. I can order by, but I can't so a sort like the
example
> above.
>
> Chuck Payne
> Magi Design and Support


One of two things to do:

When you're inserting the data, figure out the first letter and store
that as a separate column ("letter" perhaps)

Second, probably easier to implement in your case with existing data,
is to use LIKE.

$sql = "select * from datatable where last_name like '$letter%'";

The % is a wildcard symbol, so if $letter is "a" then a last name
of "adams", "aames", "aston", etc. would all match.

I know there's someway to have mysql do a string manipulation to compare
just part of a column's data with something, so you could do something
similar to a 'substr' in PHP - but it's late and I can't remember off
the top of my head.


Hope that helps...


--------------------------
Michael Kimsal
http://www.phphelpdesk.com
Taking the ? out of <?php
734-480-9961



---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail
<[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php


---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to