Jump to content


Photo

Searching by CASE in SELECT query.


  • Please log in to reply
2 replies to this topic

#1 hondaman

hondaman
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 27 February 2006 - 03:54 PM

Hi guys, nice forum. I have been playing on and off with php for awhile now but have never encountered the problem I have now and I would like to see if anyone can help me.

I am currently getting a used vehicle feed sent to me by a provider which updates my database daily. Problem is, it is not indexed nor do the tables have what i consider "real relationships". Anywho...

The problem I have is that in one table... vehicles, currently gives me the options for each vehicle using THEIR coding system which oddly enough uses upper and lower case letters as the unique id for the options table.

ie. (Options Table)
ID DESC
----- ------------------
TA Power Windows
Ta Power Door Locks
RA Keyless Entry
Ra etc.....

My question is, how can I tell my query to only select the value that matches the correct case??? I am using the query below to no avail.

$thisOptionID = "Ta";
$fq = "SELECT DISTINCT id, desc FROM options WHERE id = '$thisOptionID'";
$fr = mysql_query($fq) or die("Error in Option Select Query: ".mysql_error());
$fi = mysql_fetch_array($fr);

What it gives me back is Power Windows when I want it to give me Power Door Locks.

Please help.


#2 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 27 February 2006 - 04:19 PM

Try this:
SELECT DISTINCT id, desc FROM options WHERE id = '$thisOptionID' COLLATE latin1_general_cs

This will break the indexing, but if it works for you, you can set the `id` column to the latin1_general_cs collation permanently with ALTER TABLE. This would allow you to create a case sensitive index, and you could go back to using your original query.

#3 fenway

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

Posted 27 February 2006 - 07:55 PM

If you're using MySQL < 4.1, use the BINARY operator to make the case stick:

SELECT DISTINCT id, desc FROM options WHERE id = BINARY '$thisOptionID'

But as wickning1 indicated, if this is how you want all your queries to work, you'll have to ALTER the table to set the column to this mode.
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