Jump to content

fine tuning LIKE statements


wright67uk

Recommended Posts

Im a newbie trying to get my head around php and sql, please can anyone help me with a statement,

or point me to a place where I can find out? - have tried w3schools.

 

How can I get my like statement to only compare the first two charracters of a value?

 

Let's say $code = EN7 6PX  (user has typed this into a html form)

for eg . SELECT email FROM treesurgeons WHERE postcode LIKE '%$code%'

 

How can I tell my like statement to only take notice of the first two charracters of the post code (in this case 'EN')?

 

So that I get returned EN4 1PP, EN5 6RT, EN7 3OL etc.

Link to comment
https://forums.phpfreaks.com/topic/226859-fine-tuning-like-statements/
Share on other sites

Morning :)

 

you say it must return: EN4 1PP, EN5 6RT, EN7 3OL etc.

if your search should only look at the first 2characters of the word try out regular expressions. There are quite a few so use the proper one. but fr this one where it should look at the first 2 characters not knowing what comes behind maybe try:

 

SELECT email FROM treesurgeons WHERE postcode REGEXP '^$code'

 

have a look here for more stuff about it: http://dev.mysql.com/doc/refman/5.1/en/regexp.html

maybe also the following could work, havent tested it though:  [[:<:]]

SELECT email FROM treesurgeons WHERE postcode REGEXP ':[[:<:]]$code'

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.