how do i search an address form a group of columns from a table (zip,state,country)? - sql

scenarioi:-
1) User enters an address into a search field. Address maybe zip,state or country
2) Now i have to take that address which may be in any order or which may just contain the country, state or zip and check the database if the the results exists or not.
Any advice on this topic would be awesome

How about this one?
'SELECT * FROM table WHERE address LIKE \''. addslashes($search) .'\'
OR zip = '. (int)$search .'
OR country LIKE \''. addslashes($search) .'\''

I would do something like :
split address on space
build where clause as
WHERE zip||state||country like '%address_part_one%'
OR zip||state||country like '%address_part_two%'
and so on ...
If you have fulltext enabled for the three fields :
WHERE MATCH (zip,state,country) AGAINST ('address_part_one')
OR MATCH (zip,state,country) AGAINST ('address_part_two')
...

Related

How can I stop executing code after getting a result?

I have a textbox named "Search" and code that filters a customer out by name. I also want to display the whole table if the textbox is empty but don't know how to do it.
NOTE : I am using Microsoft Access.
Here is my code :
SELECT * FROM Customers
WHERE Forms.[Form1].[Text4] = Forms.[Form1].[Text4] AND FirstName=Forms.[Form1].[Text4];
Thank you for any help.
You need validate if text is empty or filter to another rules, this can be something like this:
SELECT * FROM Customers WHERE Forms.[Form1].[Text4] IS NULL OR FirstName = Forms.[Form1].[Text4];

ActiveRecord (SQL) query multiple columns only if search string not empty

Using a PG database filled with registered voters.
Trying to set it up so I can search by first name, last name, zip or city. I want to be able to find all voters that match all of the entered params, but having trouble dealing with empty search fields.
where("zip LIKE ? OR city LIKE ? OR last_name LIKE ? OR first_name LIKE ?",
"#{params[:zip]}","#{params[:city]}","#{params[:last_name]}","#{params[:first_name]}")
Is there a better way to build it out so that it matches ALL entered parameters, but ignores empty string parameters? Right now if I enter a first and last name 'John Smith' I will get 'John Jones' and 'Jane Smith'.
This can do the trick:
attrs_name_to_search = %w( zip city last_name first_name )
fitlered_params = params.slice(*attrs_name_to_search).select { |_,v| v.present? }
sql_cond = filtered_params.map { |k,_| "#{k} LIKE :#{k}" }.join(' OR ')
YourModel.where(sql_cond, filtered_params)
But it should return all the records if no zip/city/last_name/first_name is given.

How do I update multiple sets procedure

I've having problem with regular expression for updating the multiple set for email that doesn't work at all. You see I'm trying to update from.
alisonsmith#gmail.com
bobgraves#hotmail.com
smithers#yahoo.com
011013092949#msn.ca
011513025559#aol.ca
101513025559#MSN.COM
To the result should look like this:
alisonsmith#dony.com
bobgraves#dony.com
smithers#dony.com
011013092949#dony.com
011513025559#dony.com
101513025559#dony.com
I've tried that update procedure like this and it didn't work at all:
update dony_membership
set Email = LEFT(Email,12)+'#dony.com'
set Email = LEFT(Email,0)+'#dony.com'
where Email LIKE '%[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]#%'
or Email LIKE'#%'
As it seems like you want to change the part after the #-sign for all rows I wouldn't bother with regular expressions but rather just keep what ever comes before the #-sign and append the new domain (dony.com) to it:
update dony_membership
set email = left(email, charindex('#', email, 0)) + 'dony.com'
Sample SQL Fiddle for your viewing pleasure :)

SQL - can I use OR and LIKE on the same line of a query?

I have a search function were I can find Address Line 1 in my database, but I was wondering if it is possible to find addressline 1,2,3,4,5 which are all separate attributes on the same line, depending what the user searches for. See code below. Any help would be appreciated, Thanks.
This Works:
<sql:query var ="NameCheck" scope = "session" dataSource = "${bookdB}">
SELECT * FROM Company
WHERE Address_1 LIKE ?
<sql:param value = "%${param.category}%" />
</sql:query>
Is this possible in some form of syntax (either '||', 'OR' operator).
SELECT * FROM Company
WHERE Address_1 OR Address_2 OR Address_3 OR Address_4 OR Address_5 LIKE ?
The syntax you use is not correct.
You'll have to do something like :
SELECT * FROM Company
WHERE Address_1 LIKE ...
OR Address_2 LIKE ...
OR Address_3 LIKE ...
OR Address_4 LIKE ...
OR Address_5 LIKE ...
In Sql, yes you can use OR instead of ||, however this is the only way to select the rows in conditions, but you must complete each and every condition and then start a new condition like this:
SELECT * FROM Company
WHERE Address_1 LIKE
OR Address_2 LIKE %a OR
Address_3 LIKE %a OR
Address_4 LIKE%a OR
Address_5 LIKE %a
Computer won't change its algorithm, so you should stick to its language :)
To search multiple addresses you can use AND and then write them in the HTML. You can use OR when one condition is not true, then move to the next one. This way, you must close the condition before OR operator.
http://www.w3schools.com/sql/sql_and_or.asp

I'm in need of a sanity check on a sql query that will return a value if it exists in the table or return a default value if it doesn't

What I'm trying to do is return a mailbox location for a select few users and a catch all mailbox location for everyone else. In postfix you can pass it a query in the of the form
SELECT mailbox FROM virtual_mailboxes WHERE email = '%s'
and it will replace %s with the email user it's looking for. I have a table that contains a set of users that need their own mailbox(root, support, postmaster) and I want to be able to create a query that returns the configured mailbox or 'all/'. For example if the username is 'support' then I want it to pull the mailbox column for username 'support'. If the username is 'anybody' which doesn't match an rows in the table I want it to return 'all/'.
Here is the query I came up with.
SELECT
CASE
WHEN v2.username IS NOT NULL THEN v2.mailbox
ELSE 'all/'
END AS mailbox2
FROM virtual_mailboxes v1
LEFT JOIN virtual_mailboxes v2 ON v2.username = SUBSTRING_INDEX('support#gmail.com', '#', 1)
LIMIT 1
It works but I feel like it's very wrong to do it that way and I'm curious if there is a better way.
One other thing, postfix will only replace the first occurrence of %s. Thanks for any help.
SELECT coalesce(
(SELECT mailbox FROM virtual_mailboxes WHERE email = '%s'),
'all/'
);