Reply
mysql select first letter
Old 05-02-2008, 12:34 AM mysql select first letter
Skilled Talker

Posts: 92
I've been googling for a while and am still unsuccessful.

I'm trying to grab all the users from a table based on the first letter. Anyone have any ideas on how to do that SELECT statement?
kbfirebreather is offline
Reply With Quote
View Public Profile
 
When You Register, These Ads Go Away!
     
Old 05-02-2008, 03:20 AM Re: mysql select first letter
Mad182's Avatar
Average Talker

Posts: 21
Name: Madars
Location: Latvia
Code:
"SELECT * FROM users WHERE username LIKE 'letter%'"
But maybe there are smarter ways, how to do it...
__________________
There's no place like 127.0.0.1
Mad182 is offline
Reply With Quote
View Public Profile Visit Mad182's homepage!
 
Old 05-02-2008, 03:20 AM Re: mysql select first letter
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 861
Name: Jeremy Miller
Location: Reno, NV
PHP Code:
$start_letter 'A'//Make sure to sanitize this data if coming from user input

//Example sanitization assuming a URL of the form filename.php?start_letter=A
$start_letter substr(preg_replace('/[^a-z]/i','',$_GET['start_letter']),0,1);

$results mysql_query("SELECT field_names FROM table_name WHERE search_field_name LIKE '".$start_letter."%'");

if (
$results && mysql_num_rows($results) > 0) {
  while (
$a_result mysql_fetch_object($results)) {
    
//Do your thing with the results.
  
}

__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is online now
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-02-2008, 10:16 AM Re: mysql select first letter
Skilled Talker

Posts: 92
Is there a way to make it case insensitive?
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 05-02-2008, 10:50 AM Re: mysql select first letter
tripy's Avatar
Fetchez la vache!

Posts: 1,855
Name: Thierry
Location: In the void
Quote:
Is there a way to make it case insensitive?
Use lower() in the SQL where clause with an strtolower() php call on the parameter.
PHP Code:
$results mysql_query("SELECT field_names FROM table_name WHERE lower(search_field_name) LIKE '".strtolower($start_letter)."%'"); 
__________________
Listen to the ducky: "This is awesome!!!"


Last edited by tripy : 05-02-2008 at 10:51 AM.
tripy is offline
Reply With Quote
View Public Profile
 
Old 05-02-2008, 11:01 AM Re: mysql select first letter
Skilled Talker

Posts: 92
So when you start with a lowercase, it checks both upper and lower, but if you start with upper, it only checks fields with upper? That seems to be the case when I've been messing around with it, but I'm not for certain.

Last edited by kbfirebreather : 05-02-2008 at 11:04 AM.
kbfirebreather is offline
Reply With Quote
View Public Profile
 
Old 05-02-2008, 11:57 AM Re: mysql select first letter
tripy's Avatar
Fetchez la vache!

Posts: 1,855
Name: Thierry
Location: In the void
no. The sql part lower() tells the DB to cast any content of the field to a lowercase.
Then the seed is forced to lower case too, so you don't have to care about it
__________________
Listen to the ducky: "This is awesome!!!"

tripy is offline
Reply With Quote
View Public Profile
 
Old 05-02-2008, 01:57 PM Re: mysql select first letter
Novice Talker

Posts: 5
Name: dave patrick
Instead of coding this in php you can change the collation of the field you are searching in the database eg if your collation is set to latin1_bin change it to utf8_unicode_ci the ci stands for case insensitive
dab42pat is offline
Reply With Quote
View Public Profile
 
Old 05-02-2008, 04:08 PM Re: mysql select first letter
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 861
Name: Jeremy Miller
Location: Reno, NV
My code does do a case-insensitive search. No changes needed.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is online now
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-02-2008, 10:14 PM Re: mysql select first letter
mgraphic's Avatar
Truth Seeker

Posts: 2,285
Name: Keith Marshall
Location: West Hartford, CT
I don't think the LIKE clause in mysql is case sensitive.
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 05-04-2008, 11:14 PM Re: mysql select first letter
Average Talker

Posts: 18
Name: TK
Quote:
Originally Posted by mgraphic View Post
I don't think the LIKE clause in mysql is case sensitive.
When all else fails, consult the documentation.

http://dev.mysql.com/doc/refman/5.0/...nsitivity.html
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 05-04-2008, 11:20 PM Re: mysql select first letter
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 861
Name: Jeremy Miller
Location: Reno, NV
May want to check yourself before you start suggesting such stuff to a mod with 2000+ posts, zxcvbnm60...mgraphic was simply informing readers that the code provided with LIKE as the query is case-insensitive.

Besides, almost every post on a forum could be answered with 1) see the documentation, or 2) search Google, so those are useless replies. The third, not-so-useless, option is to hire a coder.

You will find many people who do not explicitly assert something, but, instead, suggest that the thing is likely. It is a matter of delivery and not accuracy.

Good luck as you familiarize yourself with the forum and welcome to WT.
__________________
Jeremy Miller - TeraTask Technologies, LLC
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is online now
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-05-2008, 02:54 AM Re: mysql select first letter
Average Talker

Posts: 18
Name: TK
Quote:
Originally Posted by JeremyMiller
You will find many people who do not explicitly assert something, but, instead, suggest that the thing is likely. It is a matter of delivery and not accuracy.
Pardon me JeremyMiller (and mgraphic). The above response was not directed towards mgraphic per se, but towards any readers that may be looking for definitive answers to a question. Though the regular audience of these forums may understand the customary nuances of communication that you describe, the casual reader will not. The intent of my comment was collaborative rather than correcting.

Thanks for the welcome, good sir.
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to mysql select first letter
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.18131 seconds with 13 queries