I'm having issue retrieving info from a DB by using this query with the lower() function:
SELECT DISTINCT "column_name" FROM "table" WHERE lower('car', 'house', 'plane'...) like '%owner%'
The query works with 1 attribute like for exmaple 'car' but when I try to use 100 I get the following error:
Error Code: 1582. Incorrect parameter count in the call to native function 'lower'
What should I change in order to be able to use and output more than a single attribute?
Thanks.
I think the query you want is something like this:
SELECT DISTINCT column_name
FROM yourTable
WHERE owner REGEXP 'car|house|plane'; -- and maybe other terms in the alternation
That is, you want to match all records where the owner column contains one of the substrings car, house, or plane.
Related
I have this json data in a 'name' column, where the names of some records are stored with multi-lang support:
{"en":"Professional Association","de":"Berufsverband","uk":null}
When I run this query returns 0 records:
select * from `some_table` where lower(json_value(name,'$.*')) like lower('%Berufsverband%');
But if I run the query specifying the lang key (de) it works:
select * from `some_table` where lower(json_value(name,'$.de')) like lower('%Berufsverband%');
My question is, how can I properly use the $.* wildcard on MariaDB?
If I run the exact same query on MySQL it works fine:
select * from `some_table` where lower(json_unquote(name->'$.*')) like lower('%Berufsverband%');
You can use a combination of JSON_CONTAINS and JSON_EXTRACT.
You cannot use wildcards within JSON_CONTAINS, but you can within JSON_EXTRACT, so, if you wrap the contains with an extract using a wild card (see Object Member Selector in JSONPath Expression below) you will return the rows containing the value you're searching for.
Object Member Selector
To select member(s) in a JSON object, one can use one of the
following:
.memberName selects the value of the member with name memberName.
."memberName" - the same as above but allows one to select a member with a name that's not a valid identifier (that is, has
space, dot, and/or other characters)
.* - selects the values of all members of the object.
select * from `some_table`
WHERE JSON_CONTAINS(JSON_EXTRACT(name, "$.*"), '"Berufsverband"')
db<>fiddle here.
I just made it work, I replaced json_value with json_query. The result was something like this:
select * from `some_table` where lower(json_query (name, '$')) like lower('%Berufsverband%');
Now it works as expected.
I want SQL to show / order the results for the column name first then show results for the description column last.
Current SQL query:
SELECT * FROM products WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
I tried adding order by name, description [ASC|DESC] on the end but that didn't work.
It's for optimizing the search results. If a certain word is found in description it should go last if a certain word is also found in the name column.
You can use a CASE statement in an ORDER BY to prioritize name. In the example below all results where name is matched will come first because the CASE statement will evaluate to 1 whereas all other results will evaluate to 2.
I'm not sure by your problem description what exactly you want the behavior to be, but you can certainly use this technique to create more refined cases to prioritize your results.
SELECT *
FROM products
WHERE (name LIKE '%$search_query%' OR description LIKE '%$search_query%')
ORDER BY CASE WHEN name LIKE '%$search_query%' THEN 1 ELSE 2 END
If you want the names first, the simplest order by is:
order by (name like '%$search_query%') desc
MySQL treats booleans as numbers in a numeric context, with "1" for true and "0" for false.
While this is undocumented, when results sets combined by a UNION ALL and not sorted afterwards, they stay in the order returned, as UNION ALL just adds new results to the bottom of the result set. This should work for you:
SELECT * FROM products
WHERE name LIKE '%$search_query%'
UNION ALL
SELECT * FROM products
WHERE (description LIKE '%$search_query%' AND name NOT LIKE '%$search_query%')
I apologise if the title is ambiguous.
I have a table called data and in that table is a column called barcode
The barcodes look similar to this:
TEST210001m1c8Wsa
The format of the barcode is as follows:
Job: TEST2
Auto-increment nr: 10001
Type identifier: m1
Unique ID: c8Wsa
I am trying to count the amount of barcodes in the column of the whole table that match the Job, type ID and unique ID. I am not concerned about the auto increment. I'm using the below query but it doesn't seems to work as I expect:
SELECT COUNT(*) FROM data WHERE barcode LIKE 'test2%' AND SUBSTRING('m1', LENGTH(barcode)-7,2)='m1' AND RIGHT('c8Wsa', 5)='c8Wsa'
I'm positive that I am using the SUBSTRING incorrectly because if I remove it the query returns results as expected. Could someone tell me what I'm doing wrong? I'm certain it's something small that I'm missing.
You need to specify column name for first parameter in substring() function and same for right() function also
SELECT COUNT(*) FROM data
WHERE barcode LIKE 'test2%' AND SUBSTRING(barcode, LENGTH(barcode)-6,2)='m1'
AND RIGHT(barcode , 5)='c8Wsa'
Use a sum instead like this.
Select sum(charindex, barcode, 'm1') from table where
barcode like 'Test2%' and barcode like '%c8Wsa''
That way you will effectively count all of the substrings for the barcodes filtered by your where clause.
Here is another way to do this using REGEXP:
SELECT COUNT(*)
FROM data
WHERE barcode REGEXP 'TEST2[0-9]{5}m1c8Wsa';
This assumes that the auto increment number would always be fixed at 5 digits. If it could be variable, then you may use the following pattern:
TEST2[0-9]+m1c8Wsa
I have a MySQL table column rubrics which contains string value '61,80,112,256'. So I try execute that query:
select * from table where 256 in (rubrics) and 61 in (rubrics)
And no result returns. Any suggestions?
Since your rubrics column is a comma separated list the IN operator will not work.
MySQL does have a function that can find a value in a string list so you should be able to use FIND_IN_SET():
select *
from yourtable
where find_in_set(61, rubrics)
or find_in_set(256, rubrics)
See SQL Fiddle with Demo
Something like WHERE rubrics LIKE '%,256,%' OR rubrics LIKE '256,%' OR rubrics LIKE '%,256'. Using parenthesis you can also filter on the 61, but the resulting query will be come messy. You'd better normalize your data, so you can use subqueries and joins using keys (the real deal).
(see also bluefeet's answer as FIND_IN_SET is a better approach)
Try this
select * from table where rubrics like '%'+'256,'+'%' and rubrics like '%'+'61,'+'%'
IN operator does not work with strings
use correct substring matching operator like LIKE or LOCATE
one advice - update your rubics column to begin and end with , character, that will make your LOCATE(",62,", rubics) operations unambiguous as opposed to LOCATE("62", rubics) which will match also 622 and 262 and other combinations. Locating ,62, wil fail if your rubics has value of 62,blah,foo,bar because it doesn't start with ,
I need to find rows that contain a specific number in a set of numeric values that are stored in a table. I'm using the WHERE IN() function of mysql, but I'm having problems with the proper format.
Basically I have the following query:
SELECT id,category, text
FROM ws_cat
WHERE '11' IN (category)
The category field is a VARCHAR and looks like the following:
id category
1 11
2 12,11
3 1,13,9
So I need to find the rows with id 1 and 2 in this case. Unfortunately it doesn't work and I'm guessing it's because of the missing quotes, but all the ideas of reformating with QUOTES() or just changing the format of category to something like '12','11' wouldn't work either. Both would be possible for me as long as it works...
Use the FIND_IN_SET function:
SELECT id, category, text
FROM ws_cat
WHERE FIND_IN_SET('11', category) <> 0;