TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-03-2008, 11:27 PM   #1 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 8
Thanks: 2
johnN is on a distinguished road
Default mysql query

Sorry for asking what should be a fairly simple question.

Say I have the search term "myzoo" and a database entry "zoo" how would I go about getting it to match?

Or in other terms, how can I get mysql to match the last part of a search term.


thanks very much guys, I'm incredibly stuck with this.
johnN is offline  
Reply With Quote
Old 04-04-2008, 02:06 AM   #2 (permalink)
The Wanderer
 
Sand_Devil's Avatar
 
Join Date: Nov 2007
Location: El Paso, TX
Posts: 7
Thanks: 1
Sand_Devil is on a distinguished road
Default

% is a wild card.

where blah_field like '%zoo'

Of course, if case is a problem, you can use

where upper(blah_field) like '%ZOO'
Send a message via ICQ to Sand_Devil Send a message via AIM to Sand_Devil Send a message via MSN to Sand_Devil Send a message via Yahoo to Sand_Devil
Sand_Devil is offline  
Reply With Quote
Old 04-04-2008, 02:47 AM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

MySQL is case-insensitive. Is there actually any way to tell it to be case-sensitive? Not that I need it, I'm just curious. Sand Devil is spot on though.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 04-04-2008, 12:15 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Looking at Sand_Devil's example of where upper(blah_field) like '%ZOO', by default (ie, case-insensitive) this wouldn't behave any differently (except, more slowly) to where blah_field like '%zoo'.

You can tell columns or whole tables to be case-sensitive by assigning an appropriate collation (ending in _cs [case-sensitive] or _bin [binary]). If you don't want to alter your table structure, you can change the collation, or declare the value as binary, within any given query.

Code:
Table: posts

 id  title
-----------
 1   Moo
 2   moo
 3   mop

Example Queries:

SELECT title FROM posts WHERE title LIKE 'mo%';
> 3 rows returned  (Moo, moo, mop)

SELECT title FROM posts WHERE title LIKE BINARY 'mo%';
> 2 rows returned (moo, mop)
The MySQL Manual has a page on case sensitivity in searches for a little more information.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
DeMo (04-08-2008)
Old 04-04-2008, 12:39 PM   #5 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 8
Thanks: 2
johnN is on a distinguished road
Default

Sorry, I didn't explain properly.

I want the opposite of that.

I need this string "myzoos" to match the database entry "zoos", not the other way round as I must have suggested.

I'm using ci so upper is irrelevant.

Imagine I need to use a wildcard on the actual database entries:)

Thanks:)

SELECT FROM dictionary where word LIKE '%myzoo'

will throw up things like mamyzoo, dobdomyzoo ect,. but not just "zoo"
johnN is offline  
Reply With Quote
Old 04-05-2008, 04:25 AM   #6 (permalink)
The Wanderer
 
Sand_Devil's Avatar
 
Join Date: Nov 2007
Location: El Paso, TX
Posts: 7
Thanks: 1
Sand_Devil is on a distinguished road
Default

If I understand what you are suggesting... word being the entered string and blah_field being the database entry. || allows you to concatenate strings. To search for the % you need to place an escape, usually \

where word like ('%'||blah_field)

Should do the trick.

Last edited by Sand_Devil : 04-05-2008 at 04:28 AM. Reason: Additional clarification
Send a message via ICQ to Sand_Devil Send a message via AIM to Sand_Devil Send a message via MSN to Sand_Devil Send a message via Yahoo to Sand_Devil
Sand_Devil is offline  
Reply With Quote
The Following User Says Thank You to Sand_Devil For This Useful Post:
johnN (04-05-2008)
Old 04-05-2008, 08:12 PM   #7 (permalink)
The Wanderer
 
Join Date: Apr 2008
Posts: 8
Thanks: 2
johnN is on a distinguished road
Default

Quote:
Originally Posted by Sand_Devil View Post
If I understand what you are suggesting... word being the entered string and blah_field being the database entry. || allows you to concatenate strings. To search for the % you need to place an escape, usually \

where word like ('%'||blah_field)

Should do the trick.
You are instantly and permanently my favorite person.

I had to use CONCAT but the details you gave me helped me solve it within minutes!

Thank!
johnN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:54 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design