selecting from mySQL database first letter

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

selecting from mySQL database first letter

Post by psychotomus »

how can I do something like this?

$result = mysql_query( "SELECT * FROM wallpaper_cats WHERE sub_cat FirstLetterA" );//
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

interesting. maybe try

Code: Select all

$sql = ......'WHERE thing = "'.$looking_for_letter.'"* LIMIT 20';
or whatever. That is off the top of my head as what might work but odds are it does not. worth a shot though
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

I believe MySql's string function LEFT() is what you're looking for.

http://dev.mysql.com/doc/refman/5.0/en/ ... #id3219420
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Are you trying to truncate the first letter of a know data value down to it's first letter and match that? If so, use the MySQL LEFT() function...

Code: Select all

<?php
$sql = "SELECT LEFT(`fieldname`, 1) AS first_letter FROM tablename WHERE first_letter = 'A')";
?>
EDIT | Darnit Hawley, you beat me to the punch. :)
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Everah wrote:Are you trying to truncate the first letter of a know data value down to it's first letter and match that? If so, use the MySQL LEFT() function...

Code: Select all

<?php
$sql = "SELECT LEFT(`fieldname`, 1) AS first_letter FROM tablename WHERE first_letter = 'A')";
?>
EDIT | Darnit Hawley, you beat me to the punch. :)
You will not be able to use the alias in the conditional statement.

Code: Select all

<?php
$sql = "SELECT LEFT(`fieldname`, 1) AS first_letter FROM tablename WHERE LEFT(`fieldname`, 1) = 'A')";
?>
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: selecting from mySQL database first letter

Post by timvw »

Code: Select all

SELECT blah
FROM wallpaper_cats
WHERE sub_cat LIKE 'A%'
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Sometimes being simple is so complicated. Thanks timvw. That basic thought never entered my mind. :cry:
Post Reply