tags:

views:

57

answers:

3

Hi I am having difficulties writing a perfect sql query that will search for an address or postcode(english).

I stored my postcode as "W1Y TF7". If a user searches using "W1YTF7" (without the space), it does give me any result when it is suppose to return W1Y TF7.

VENUE

  • VenueID
  • Name
  • Address1
  • Address2
  • City
  • Postcode
  • PhoneNumer
  • URL

here is my query:

SELECT DISTINCT *
 FROM venue 
WHERE Name LIKE '%$searchValue%' 
   OR Postcode LIKE '%$searchValue%'
   OR Address2 LIKE '%$searchValue%' 
   OR Postcode LIKE '$searchValue%' 
+2  A: 

I would probably store the postcodes in the database without the space. Then you could simply strip the spaces out of the string before running your SQL query.

If you want to keep your current setup, why not convert the input into your expected format BEFORE it gets to the SQL level? Just add the space if it's not there.

Paul McMillan
Simple, and elegant solution.
Lucanos
I thought about doing this but the problem is since I dont know what the user is search for cause the user can search for an address or postcode, I cant start removing spaces. For example if the user searches '19 arygll street' I cant start removing spaces cause it would produce something unregonizable.
Then set the query to match the string with and without spaces?
Paul McMillan
A: 

well, the first pitfall: I could see you run into some problems here if things are uninitiated (if $searchValue is an empty string or a very small string). This would always return a very large dataset (and might take quite some time).

for the postcode, you could do (LEFT(Postcode, 3) = LEFT($searchValue, 3) AND RIGHT(Postcode, 3) = RIGHT($searchValue, 3))

also, you might want to look into prepared statements and value binding, but that's a different problem altogether

Martijn
I just tried this and it is saying error in sql syntax. is the LEFT an sql sntax?
I'm not quite sure if it's ISO SQL, but I do know it's there in MySQL and T-SQL (not sure about PL/SQL) (with this syntax, see http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_left ) Could you give the actual SQL statement you're trying?
Martijn
MySql might expect backticks though (i'm never sure of those).
Martijn
A: 

You can make a search column, create a new field that you call SearchColumn the venue table, then make a SELECT and UPDATE trigger that sets the same values in all the fields you want to use (VenueID + Name + Address1 + Address2 + City + Postal Code + PhoneNumer + URL). ex. if you insert. It's also nice to have this included as an index.

Then you need to:

SELECT DISTINCT Name
      FROM venue
       WHERE SearchColumn LIKE '%Search value%'
sv88erik
Sry I am confused. i cant see the bigger picture, how that helps with the search. Pls if u dont mind, can u explain further
the idea here is that you add a column searchColumn, that would contain for example: "Place xW1Y TF7foostreet 77Manchester" and you would do where searchColumn LIKE '%user input%'. This doesn't help you with the possibly spaced postal code though
Martijn