SQL two or and one and in the where function - sql

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

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";

Using multiple OR LIKE statements

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 ?)

jpa postgres - query result difference with like on TEXT fields

I'm trying to make a query to search items structured as follows:
IssueCategory --* Issue (one to many)
using the following JPQL
select count(z) from IssueCategory z join z.issues x
where
lower(cast(function('lo_get', cast(x.diagnosis as integer)) as text)) like lower(concat('TEXT TO SEARCH', '%'))
where diagnosis is a Issue's String field with #Lob annotation, mapped as a text field in postgres:
CREATE TABLE issues (
...
diagnosis text,
...
)
this query produces the following query
select count(issuecateg0_.id) as col_0_0_
from issue_categories issuecateg0_
inner join issues issues1_ on issuecateg0_.id=issues1_.category_id
where lower(cast(lo_get(cast(issues1_.diagnosis as int4)) as text)) like lower(('TEXT TO SEARCH'||'%'))
Obviously in origin the "TEXT TO SEARCH" was passed as a parameter to the query.
The problem is: when I execute the JPQL query, it returns 0, but if I execute the generated query directly in postgres, I get 1.
Does anyone know of behaviours like this one?
I finally changed to the following conditions:
lower(function('encode', (function('lo_get', cast(x.diagnosis as integer))), 'escape') like lower(concat('TEXT TO SEARCH', '%'))

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