SUBSTRING in COUNT query not working as expected - mysql

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

Related

Issue revtrieving 100 elements using the lower() function

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.

Cannot check if a field contains also number

I'm trying to check if a field in a specific table contains also number, in particular I have a record that have the field name which contains this value: Besëlidhja Lezhë vs. Tërbuni Pukë 1 - 1, so I'm trying to get also all the rows of that table that contains a number inside the field name. I tried:
SELECT * FROM `venue` where `name` like '%[0-9]%'
but this will return an empty result, any idea?
This should tell you if name contains any digit (not tested)
SELECT * FROM venue WHERE name REGEXP '[0-9]'
You can try using a Regular Expression that filters for names in your name column with numeric characters . For example:
SELECT * FROM DATA WHERE name REGEXP '[a-z]...[0-9]';
mySQL allows you to use regular expression as a filter !
This should select out for names like Tërbuni Pukë 1 - 1. If you want to practice regular expressions this is a great website to test whether you have the right regex. https://regex101.com/
Hope this helps !
I believe this should work:
SELECT *
FROM venue
WHERE name like '%0%' or name like '%1%' or name like '%2%' or name like '%3%'
and so on til you get to 9. I hope this helps

SQL - Query to find if a string contains part of the value in Column

I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?

MySQL query to get codes of countries

I am developing an application, in one part of it I get a string as the phone number and I have to return the phone provider of that number, in database I have table prefix with this structure:
id , name, code
Some data in it is like:
(23242, 'UK-Mobile-T Mobile', '447984'),
(23243, 'UK-Mobile-T Mobile', '447985'),
(23244, 'UK-Mobile-T Mobile', '447986'),
(23245, 'UK-Mobile-T Mobile', '447987'),
(23246, 'UK-Mobile-Vodafone', '447407'),
(23247, 'UK-Mobile-Vodafone', '447423'),
name is the provider and code is the prefix belongs to that provider
and what I get as input is a phone number just like 447243xxxxx
Question is this:
how should I create a query to return the UK-Mobile-Vodafone as a result when the above input is given ?
please remember length of this code is not same for every country
This may work for you:
select t.*
from table t
where '447243xxxxx' like concat(t.code, '%');
This assumes, among other things, that only one prefix matches each number. Otherwise you need to choose among them.
If you need to choose among them, typically you would want the longest matching one:
select t.*
from table t
where '447243xxxxx' like concat(t.code, '%')
order by length(t.code) desc
limit 1;
And then, if you want to be able to use an index, you don't want to use concat() on the code. Instead, extract the first n characters. This is easy if all have the same length (6) as in your example:
select t.*
from table t
where left('447243xxxxx', 6) = t.code;
Just use the LIKE-operator with GROUP BY:
SELECT NAME FROM TBL_TABLENAME WHERE CODE LIKE '447243%' group by NAME;

Prepare mysql row for use in IN() clause

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;