I have columns title, description, keywords and I want to search a text in them.
I'm using PostgreSQL.
SELECT title FROM products
WHERE
ANY(ARRAY(title, description, keywords) like '%test%')
I'm getting error syntax error at or near "ANY".
I know I can do it with OR but I think that would not be the best way to do it.
I think I'm having errors at converting columns to array. Can someone give me an idea?Thanks!
Presumably, your goal is to only have the comparison '%test%' appear once. Here is one method:
WHERE EXISTS (SELECT 1
FROM UNNEST(ARRAY[p.title, p.description, p.keywords]) x
WHERE x LIKE '%test%'
)
Note that if you using equality, then IN suffices:
WHERE test IN (p.title, p.description, p.keywords)
Related
I'm looking for a way to accomplish something kind of like fuzzy search with Oracle. If this has already been answered, I'll gladly accept a link, but I'm so new to Oracle that I'm not even sure how to quickly search for what I want.
Given the following query:
SELECT VEND_CUST_CD, LGL_NM, ALIAS_NM
FROM {{DB_NAME}}.{{DB_TABLE}}
WHERE ({{condition_1}}) AND ({{condition_2}}) AND (upper(LGL_NM) LIKE upper('%{{term}}%')
ORDER BY LGL_NM
What I'd like to get in my response is a particular order. Let's imagine term=ze for the purposes of this.
I'd like to get results ordered like so:
Zealot Johnson
Zebra Eaters
Zero Gravity
Amazed John
Bedazzel
Lazer Sex
Zazew
So that what I'm getting back first is words that start with term followed by an alphabetical list of words that contain term within them.
I hope this is clear.
I suppose you can order the results like so:
ORDER BY CASE WHEN LGL_NM LIKE '{{term}}%' THEN 1 ELSE 2 END, LGL_NM
You can use union all like this:
select * from (
SELECT VEND_CUST_CD, LGL_NM, ALIAS_NM
FROM {{DB_NAME}}.{{DB_TABLE}}
WHERE ({{condition_1}}) AND ({{condition_2}}) AND (upper(LGL_NM) LIKE upper('{{term}}%')
union all
SELECT VEND_CUST_CD, LGL_NM, ALIAS_NM
FROM {{DB_NAME}}.{{DB_TABLE}}
WHERE ({{condition_1}}) AND ({{condition_2}}) AND (upper(LGL_NM) LIKE upper('{{%term%}}') AND (upper(LGL_NM) not LIKE upper('{{term%}}')
) order by ...
or like #Salman A 's way
Is there a way to create a where statement using like with two words (or more) but they are not in the same order as the words given?
select
i.ITEM_NUMBER as Item,
min(i.ITEM_DESCRIPTION) as Description,
i.ORGANIZATION_CODE as Org,
max(m.MANUFACTURER_NAME) as Manufacturer,
max(m.MANUFACTURERS_PARTNUMBER) as Partnumber
from mis.XXEAM_INVENTORY_REPORT_WITH_LOCATORS_CONSIGNED_MATERIAL_AND_CATEGORIES_TBL i
left outer join
mis.XXEAM_MANUFACTURING_PART_NUMBERS_TBL m on i.ITEM_NUMBER = m.ITEM_NUMBER
where
i.ITEM_DESCRIPTION like '%ALLEN BRADLEY%PLC%'
group by i.ORGANIZATION_CODE, i.ITEM_NUMBER
I would like to search the description text above as the '%ALLEN BRADLEY%PLC%' or '%PLC%ALLEN BRADLEY%'. I would like to avoid using OR statements because the dataset is huge and the query can take a long time.
The wildcards can be more than two, I just took those two as an example. In my mind I would for instance prefer to fetch the dataset for the first word and then fetch from that dataset the second word. Perhaps the only way is to select into temp-table.
You can do two LIKE for 'ALLEN BRADLEY' and 'PLC'. It should work for what you want:
SELECT
i.ITEM_NUMBER AS Item
,MIN(i.ITEM_DESCRIPTION) AS Description
,i.ORGANIZATION_CODE AS Org
,MAX(m.MANUFACTURER_NAME) AS Manufacturer
,MAX(m.MANUFACTURERS_PARTNUMBER) AS Partnumber
FROM mis.XXEAM_INVENTORY_REPORT_WITH_LOCATORS_CONSIGNED_MATERIAL_AND_CATEGORIES_TBL i
LEFT OUTER JOIN mis.XXEAM_MANUFACTURING_PART_NUMBERS_TBL m
ON i.ITEM_NUMBER = m.ITEM_NUMBER
WHERE i.ITEM_DESCRIPTION LIKE '%ALLEN BRADLEY%'
and i.ITEM_DESCRIPTION LIKE '%PLC%'
GROUP BY i.ORGANIZATION_CODE
,i.ITEM_NUMBER
you can use:
PATINDEX ('((.*)[ALLEN BRADLEY](.*)[PLC](.*))|('(.*)[PLC](.*)[ALLEN BRADLEY](.*)'),
i.ITEM_DESCRIPTION);
Depending on your need for a performatic, powerful query, it makes sense to create a full-text index on the column. It will benefit your query by using the index and also give you the flexibility when including new items to the condition. If the query is small, the LIKE will be enough, but for long tables where time is a problem, create a full-text index.
WHERE CONTAINS (i.ITEM_DESCRIPTION,"'ALLEN BRADLEY' OR 'PLC' OR 'any other item searched'");
I am getting very strange behavior on 2.0-M2. Consider the following against the GratefulDeadConcerts database:
Query 1
SELECT name, in('written_by') AS wrote FROM V WHERE type='artist'
This query returns a list of artists and the songs each has written; a majority of the rows have at least one song.
Query 2
Now try:
SELECT name, count(in('written_by')) AS num_wrote FROM V WHERE type='artist'
On my system (OSX Yosemite; Orient 2.0-M2), I see just one row:
name num_wrote
---------------------------
Willie_Cobb 224
This seems wrong. But I tried to better understand. Perhaps the count() causes the in() to look at all written_by edges...
Query 3
SELECT name, in('written_by') FROM V WHERE type='artist' GROUP BY name
Produces results similar to the first query.
Query 4
Now try count()
SELECT name, count(in('written_by')) FROM V WHERE type='artist' GROUP BY name
Wrong path -- So try LET variables...
Query 5
SELECT name, $wblist, $wbcount FROM V
LET $wblist = in('written_by'),
$wbcount = count($wblist)
WHERE type='artist'
Produces seemingly meaningless results:
You can see that the $wblist and $wbcount columns are inconsistent with one another, and the $wbcount values don't show any obvious progression like a cumulative result.
Note that the strange behavior is not limited to count(). For example, first() does similarly odd things.
count(), like in RDBMS, computes the sum of all the records in only one value. For your purpose .size()seems the right method to call:
in('written_by').size()
I am confused by the results of a query I am running. Hopefully this doesn't end with me slapping my head and feeling like an idiot, but here goes (SQL Server 2008).
First query was this:
SELECT p.product_number,p.long_desc
FROM products p
WHERE p.prod_status = 1
AND ((p.long_desc IS NULL) OR (p.long_desc LIKE '%N/A%'))
ORDER BY p.product_number
Second version is this:
SELECT p.product_number,p.long_desc
FROM products p
WHERE p.prod_status = 1
AND p.long_desc IS NULL
OR p.long_desc LIKE '%N/A%'
ORDER BY p.product_number
There are three products in the second version that do not appear in the first, yet to me these two queries should give identical results. The three items that appear in the second but not the first all have the value N/A in the long_desc column.
However there are many others with N/A as well and show up in both versions.
What don't I understand about the use of parenthesis here?
AND has a higher precedence than OR (see documentation), so without the parentheses, the query is equivalent to this:
SELECT p.product_number,p.long_desc
FROM products p
WHERE (p.prod_status = 1
AND p.long_desc IS NULL)
OR p.long_desc LIKE '%N/A%'
ORDER BY p.product_number
SELECT bp.*,r.rating,COUNT(r.review_for),bp.business_name,bp.profile_member
FROM ibf_business_reviews r
LEFT JOIN ibf_business_profiles bp ON ( r.review_for=bp.profile_member )
WHERE bp.sub_category LIKE '%{$id},%'{$location_sql}
GROUP BY r.review_for HAVING COUNT(r.review_for) >=1
ORDER BY r.date_posted DESC LIMIT 0,2");
This query is used to show results for business_name in a certain sub_category id '%{$id} in a certain location. My problem is that extra results are showing in categories that share a second or third digit aka ...viewcat&id=54 will show in ..viewcat&id=154 etc
I using the LIKE may be my issue? WHERE bp.sub_category LIKE '%{$id},%'
You are storing a comma-separated list in a varchar, when you should store one number per row in a child table. Then you wouldn't have to use LIKE at all.
Read up on First Normal Form.
Here was my comment
+! for the need to reformat the SQL. You do realize that the "percent"
signs (%) are the wildcards. So you're
essentially telling it that you can
return ANYTHING that includes id... so
if you search "23" you could get
"123", you could get "234" or
"1234"... etc.
and you replied
Thanks #Rock removing the wildcards worked!
Now my answer to this is... If you removed BOTH wildcards from your string, then you're essentially doing an "equals".
IE:
WHERE bp.sub_category LIKE '{$id},'
should be the same as
WHERE bp.sub_category = '{$id},'
Because you don't have any wildcards to "match" in the "LIKE" statement.
Please forgive me if I screwed up the "$" or the ","... I'm not a MySQL guy