Using multiple OR LIKE statements - sql

I have a database and I want to filter for some entries. This is how the filter looks like:
SELECT * FROM Item WHERE 1=1 AND Favorite = 1 AND ( DocType = ? OR DocType = ? OR DocType = ? OR DocType = ? ) AND (Title Like ? OR Location LIKE ? OR DocType LIKE ? OR ProType LIKE ?)
The filter almost works. I want to focus on the last part:
AND (Title Like ? OR Location LIKE ? OR DocType LIKE ? OR ProType LIKE ?)
Because it seems like
OR DocType LIKE ? OR ProType LIKE ?
is ignored. When switching Location with DocType for example:
AND (Title Like ? OR DocType LIKE ? OR Location LIKE ? OR ProType LIKE ?)
I can find anything related to DocType. Location can't be found anymore (just as ProType). So I guess I wrote it down wrong.
How would I need to write it down so I can filter for Title, Location, DocType and ProType?

You have two checks against the DocType column, one with = and one with LIKE and since you have added % to your search string the = will most likely return false so nothing will be returned since the two checks are grouped with an AND so your WHERE condition will become (shortened)
WHERE DocType = '%ABC%' AND DocType LIKE '%ABC'
I think you should simply remove the part directly before the AND in your query
WHERE 1=1 AND Favorite = 1 AND (Title Like ? OR Location LIKE ? OR DocType LIKE ? OR ProType LIKE ?)

Related

SQL - Search multiple columns

I'm trying to search multiple columns for similar/matching values, while ensuring the "UID" is unique.
This seemed simple, but the following query ignores the "UID" and will return anything that matches under "SalesRef, "CustomerPO" or "Ref".
Any ideas why this would be?
SELECT * FROM OrderHeader
INNER JOIN DespatchDetails ON DespatchDetails.Ref = OrderHeader.Ref
INNER JOIN OrderStatus ON OrderStatus.Ref = OrderHeader.Ref
WHERE OrderHeader.UID = '$uid'
AND OrderStatus.SalesRef LIKE '%$search%'
OR OrderStatus.CustomerPO LIKE '%$search%'
OR OrderStatus.Ref LIKE '%$search%'
ORDER BY OrderHeader.OrderDate DESC";
There is a problem in the WHERE condition of yours. Consider to use brackets every time when you're using OR in WHERE part of the query
Let's have a closer look to your version
...
WHERE OrderHeader.UID = '$uid'
AND OrderStatus.SalesRef LIKE '%$search%'
OR OrderStatus.CustomerPO LIKE '%$search%'
...
To the database it says: get me all the lines where
WHERE OrderHeader.UID = '$uid'
AND OrderStatus.SalesRef LIKE '%$search%'
or
OrderStatus.CustomerPO LIKE '%$search%'
it means when this "OrderStatus.CustomerPO LIKE '%$search%'" condition is true, it will return a row without checking for the UID column and so on.
As far as I can understand the logic, all you need is to update the WHERE part of the query with the following
WHERE OrderHeader.UID = '$uid'
AND (OrderStatus.SalesRef LIKE '%$search%'
OR OrderStatus.CustomerPO LIKE '%$search%'
OR OrderStatus.Ref LIKE '%$search%')
ORDER BY OrderHeader.OrderDate DESC";

MS-Access query - Including Null values in with other conditions

Please help as this is aquery with many parameters but when it said
" ((OrderProdDetails.DeliveryDate) Like IIf([Forms]![ReportCriteriaSelection]![NoDevDate]=-1,"*","")) " it should have included null values aswell but it is not if I am putting or Is null with this criteria then it is being applied for both conditions regardless of check box is checked or not. Please help getting the null values included when checkbox is checked.
Please have look on screenshots:
Query in design View
Criteria Selection Screen
Query:
SELECT OrderCustMain.OdrID, OrderCustMain.CustName, OrderCustMain.CustEmail,
OrderCustMain.OdrDate, OrderProdDetails.DeliveryDate, Date()-
[OrderCustMain]![OdrDate] AS [Days Past], OrderCustMain.OrderStatus,
OrderProdDetails.BrandName, OrderProdDetails.ModelName,
OrderProdDetails.Priority, SupplierDetails.SupEmail, OrderProdDetails.Status
FROM SupplierDetails INNER JOIN (OrderCustMain INNER JOIN OrderProdDetails
ON OrderCustMain.[OdrID] = OrderProdDetails.[OrdID]) ON
SupplierDetails.SupOrgName = OrderProdDetails.BrandName WHERE
(((OrderCustMain.OdrDate) Like IIf([Forms]![ReportCriteriaSelection]!
[NoStDate]=-1,"*","")) AND ((OrderProdDetails.DeliveryDate) Like
IIf([Forms]![ReportCriteriaSelection]![NoDevDate]=-1,"*","")) AND ((Date()-
[OrderCustMain]![OdrDate])>=[Forms]![ReportCriteriaSelection]![cboDaysPast])
AND ((OrderCustMain.OrderStatus) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) OR
(((OrderCustMain.OdrDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritOdrStDate] And (OrderCustMain.OdrDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritOdrEndDate]) AND
((OrderProdDetails.DeliveryDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritDlvryStDate] And (OrderProdDetails.DeliveryDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritDlvryEndDate]) AND ((Date()-
[OrderCustMain]![OdrDate])>=[Forms]![ReportCriteriaSelection]![cboDaysPast])
AND ((OrderCustMain.OrderStatus) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) OR (((OrderCustMain.OdrDate)
Like IIf([Forms]![ReportCriteriaSelection]![NoStDate]=-1,"*","")) AND
((OrderProdDetails.DeliveryDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritDlvryStDate] And (OrderProdDetails.DeliveryDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritDlvryEndDate]) AND ((Date()-
[OrderCustMain]![OdrDate])>=[Forms]![ReportCriteriaSelection]![cboDaysPast])
AND ((OrderCustMain.OrderStatus) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) OR
(((OrderCustMain.OdrDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritOdrStDate] And (OrderCustMain.OdrDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritOdrEndDate]) AND
((OrderProdDetails.DeliveryDate) Like IIf([Forms]![ReportCriteriaSelection]!
[NoDevDate]=-1,"*","")) AND ((Date()-[OrderCustMain]![OdrDate])>=[Forms]!
[ReportCriteriaSelection]![cboDaysPast]) AND ((OrderCustMain.OrderStatus)
Like IIf(IsNull([Forms]![ReportCriteriaSelection]![RepCritOdrStatus]),"*",
[Forms]![ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) ORDER BY
OrderCustMain.OdrID, OrderProdDetails.Priority;
The primary source for your trouble is that dates are not strings, so "Date Like some-string" will either fail or return unintended results.
You'll have to redesign this from scratch. Exactly how depends on the context.
Would something like this help?
WHERE Nz(OrderCustMain.OdrDate, #1/1/1900#) >= IIf([Forms]![ReportCriteriaSelection]!
[NoStDate]=-1,Nz(OrderCustMain.OdrDate, #1/1/1900#),[Forms]![ReportCriteriaSelection]!
[RepCritOdrStDate])
It might simplify the code.

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}%")

SQL two or and one and in the where function

I have this search function that has two column. The search button is working except that it includes column that are hidden. How do I fix the SQL? This is my SQL:
SELECT app_keyword.id, app_keyword.visibility,app_keyword.keyword,application_table.app_name
FROM appwarehouse.APP_KEYWORD
INNER JOIN application_table
ON application_table.id = app_keyword.app_id
WHERE app_name like '%$search%' OR keyword like '%$search%'
AND app_keyword.VISIBILITY != 'hidden';"
It keeps on displaying columns that are hidden. How do I fix this?
This issue is due to priority of logic operators (AND has precedence over OR). Try the following :
WHERE (app_name LIKE '%$search%'
OR keyword LIKE '%$search%')
AND app_keyword.VISIBILITY != 'hidden'
In your query it is executed as
WHERE app_name LIKE '%$search%'
OR (keyword LIKE '%$search%'
AND app_keyword.VISIBILITY != 'hidden')

Compare issue in yii

In almost in every wiki simple things are explained. I'm stuck in yii's CDbcriteria compare issue.
Only the exact "equal to" match is explained
for:
select * from users where status ='active'
This comparison is explained:
$criteria->compare('status','active');
But I can't find an example script which describes it with an operator based search. Like not equal to for the following query:
select * from users where status !='active'
How can I do this?
try some thing like this
$criteria->condition = " status<>'active'";
$criteria->compare('status',$this->status,true);
$criteria->addNotInCondition('status', array('active'));
if count(array('active')) === 1 sql will status != 'active', else status NOT IN ('active', 'smth else')
Try
$criteria->addCondition("NOT status = 'active'");
I had the below 3 conditions and it works like a charm:
$criteria->condition='employee_id<>:employee_id AND vehicle_id=:vehicle_id AND checked_in_on IS NULL';
$criteria->params=array(':employee_id'=>Yii::app()->session['activity']->employee_id,':vehicle_id'=>$model->id);
$checkedOutVehicleBySomeoneElse = Activity::model()->find($criteria);