Page 1 of 1

selecting from mySQL database first letter

Posted: Mon May 22, 2006 2:13 pm
by psychotomus
how can I do something like this?

$result = mysql_query( "SELECT * FROM wallpaper_cats WHERE sub_cat FirstLetterA" );//

Posted: Mon May 22, 2006 3:02 pm
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

Posted: Mon May 22, 2006 3:08 pm
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

Posted: Mon May 22, 2006 3:09 pm
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. :)

Posted: Mon May 22, 2006 3:11 pm
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')";
?>

Re: selecting from mySQL database first letter

Posted: Mon May 22, 2006 4:45 pm
by timvw

Code: Select all

SELECT blah
FROM wallpaper_cats
WHERE sub_cat LIKE 'A%'

Posted: Mon May 22, 2006 4:47 pm
by RobertGonzalez
Sometimes being simple is so complicated. Thanks timvw. That basic thought never entered my mind. :cry: