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

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

Related

SQL Multiple WHERE Columns combined with LIKE

after searching through the whole www I finally ended up here with a question and hopefully more capable responders than I am.
I am trying to implement a full text search on my webpage.
The Sql query works with one WHERE condition but not with several. Unfortunately I have no clue how to solve my problem with the help of EXIST / IN / INNER JOIN or other Operators.
Code which works:
$sql = "SELECT * FROM artikel WHERE author LIKE '$suchtext' LIMIT $offset, $no_of_abs";
Code which doesnt work:
$sql = "SELECT * FROM artikel WHERE (author OR shorty OR vollartikel) LIKE '%$suchtext%' LIMIT $offset, $no_of_abs";
The variable $suchtext is correctly passed and received through jquery $ajax POST and my php script.
Example goal:
author | volltext | shorty
--------+-------------+---------
tulum | tul | asf
rae | zutotu | vizetu
$suchtext is "tu"
Results: 2
Row1 & Row2
I appreciate any answers.
Solution:
$sql = "SELECT * FROM artikel WHERE author LIKE '%$suchtext%' OR shorty LIKE '%$suchtext%' OR vollartikel LIKE '%$suchtext%' LIMIT $offset, $no_of_abs";
LIKE is a binary operator that takes two strings. OR is a boolean operator that operates on boolean values.
You need to explicitly list this out:
(author LIKE ? OR
shorty LIKE ? OR
vollartikel LIKE ?
)
Note that this uses ? as a parameter placeholder. Use parameters when constructing SQL queries in applications.

Rails SQL: Creating a query dynamically

I am trying to create a dynamic query based on User inputs
if a
conditions << ["fname like (?)"]
sql_params << "abc"
end
if b
conditions << ["lname like (?)"]
sql_params << "def"
end
if c
cconditions << ["middle like (?)"]
sql_params << "xyz"
end
results = Model.where(conditions.join(' AND '), sql_params)
While the conditions get in correct syntax, the sql_params are listed as an array. This forms the below query
Model.where("fname like (?) AND lname like (?) AND middle like (?)", ["abc","def","xyz"])
while what I need is
Model.where("fname like (?) AND lname like (?) AND middle like (?)", "abc","def","xyz")
I tried several map/join etc options on the sql_params array but nothing worked.
Are you sure you need to produce exactly this?
Model.where("fname like (?) AND lname like (?) AND middle like (?)", "abc","def","xyz")
Something of the form:
Model.where('expr1 AND expr2 AND expr3')
is equivalent to:
Model.where('expr1').where('expr2').where('expr3')
so why not build the query piece by piece rather than messing around with strings? Something like this:
query = Model.all
query = query.where('fname like ?', 'abc') if a
query = query.where('lname like ?', 'def') if b
query = query.where('middle like ?', 'xyz') if c
will give you the same result.
You need to pass sql_params with * (known as splat operator) i.e.
results = Model.where(conditions.join(' AND '), *sql_params)

SQL and search in many column

My sql in rails
#search = #search.where('txt_1 OR txt_2 OR keywords like ?', some_value)
Why this doesn't work. When I have only " txt_1 OR txt_2 " - i think it works ok. But when I add next OR, there is no result when some_value doesn't exist in last OR.
ANSWER:
ok as I see, OR is condition that return true for one OR/AND second variable. I make this like that, and it works - I don't know is this a good solution
#items.where(' (txt_1 OR txt_2 like ?) OR (keywords like ?) ', "%#{search_name}%","%#{search_name}%")
The OR statement in SQL has to be used to compare different assertion, like something = 'something' OR other_thing = 'other_stuff'.
In your case, you should use the OR keyword as the following:
#items.where('txt_1 LIKE ? OR txt_2 LIKE ? OR keywords LIKE ?', "%#{search_name}%","%#{search_name}%", "%#{search_name}%")
But since you use the same value (search_name), you can use this pretty usefull syntax:
#items.where('txt_1 LIKE :search OR txt_2 LIKE :search OR keywords LIKE :search', search: "%#{search_name}%")

Rails query join on regex

This is a query run very infrequently (as admin), and it's not worth indexing/ using up RAM with sphinx.
So, with the variables set up something like this:
fn, ln, em = 'John', 'Smith', 'johnsmith#gmail.com'
The query is something like this:
profiles = Profile.joins(:user).where(:users=>{"email" => em}).
where("first_name LIKE '%#{fn}%' AND last_name LIKE '%#{ln}%'")
Except that I want the email to accept a substring (e.g. if em == 'th#gm', then it should match 'johnsmith#gmail.com'). How do you write this to do a join selecting only those users with a variable matching a regex?
(I got my inspiration from here):
Have you tried something like:
profiles = Profile.joins(:user).where("first_name LIKE ? AND last_name LIKE ?", "%#{fn}%", "%#{ln}%")
profiles.select{|p| p.user.email =~ /#{em}/}

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

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')
...