Jump to content


Photo

mysql query A-Z


  • Please log in to reply
4 replies to this topic

#1 esport

esport
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 18 July 2006 - 12:10 AM

Hi,
I'm trying to do query that matches the first character of a feild. Rather than selecting all rows from the table then doing an 'if' statement to filter out the rows you dont wont, is there a way doing a mysql query that only matches the first character?
Thanks

Daniel

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 18 July 2006 - 12:27 AM

SELECT * FROM tablename WHERE left(somefieldname,1) = 'A' ... for example
Legend has it that reading the manual never killed anyone.
My site

#3 esport

esport
  • Members
  • PipPipPip
  • Advanced Member
  • 98 posts

Posted 18 July 2006 - 12:47 AM

Thanks mate, works great. I guess its not case sensitive?

Daniel

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 18 July 2006 - 01:29 AM

Thanks mate, works great. I guess its not case sensitive?


Correct. Did it need to be? If so, I *think* you need to modify the query to ... BINARY left(somefieldname,1) = 'your choice of case-sensitive letter'
Legend has it that reading the manual never killed anyone.
My site

#5 fenway

fenway
  • Staff Alumni
  • MySQL Si-Fu / PHP Resident Alien
  • 16,199 posts
  • LocationToronto, ON

Posted 18 July 2006 - 08:01 AM

You can also use a simple REGEXP for this, though I've not done the performance comparisons... personally, I find REGEXs easier to read, but I spend all my time staring at Perl code, so I'm biased.  BTW, MySQL 4.1+ requires the use of a COLLATION to get the old binary attribute, AFAIK.
Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users