i have a simple (i hope its simple) question.
In my database, i have an entry like this:
Now, i need a response with true or a count if the '514' in 'error_code' is in the string 'count_alarm'. In this example it returns zero because 514 isnt in count_alarm.
I beginns the query, but i dont know how i can solve this query:
select count(*) from table where sID='56df32a1463d4387' and [if
error_code in count_alarm then True]
Somebody an idea?
Perhaps you can just use REGEXP here:
SELECT COUNT(*) AS cnt
FROM yourTable
WHERE sID = '56df32a1463d4387' AND
count_alarm REGEXP CONCAT('[[:<:]]', error_code, '[[:>:]]');
find_in_set can parse comma separated fields:
select count(*)
from your_table
where sID = '56df32a1463d4387'
and find_in_set(error_code, replace(count_alarm, '|', ',')) > 0
or use instr
where sID = '56df32a1463d4387'
and instr(count_alarm, concat('|', error_code, '|')) > 0
select count(sID) as n514 from table where sID='56df32a1463d4387'
and count_alarm like '%|514|%'
The LIKE operator searches for the pattern |514| anywhere in the value under count_alarm.
The assumption is that the first and last character of that value is this character | else 514 would not be found if it is the first or last pattern within that value.
Related
(MySQL)
I have a query to check if 'phone_number' or 'fax_number' startsWith a value from a given array,
lets say const possibleValues = [123,432,645,234]
currently my query runs with the 'or' condition, to check if -
'phone_number' or 'fax_number' that starts with 123
or
'phone_number' or 'fax_number' that starts with 432
or
'phone_number' or 'fax_number' that starts with 645
or
'phone_number' or 'fax_number' that starts with 234
it runs extremely slow on a big database, and I wish to make it faster,
is there a way to make it run faster?
I'm kinda new to sql queries,
any help would be highly appreciated!
You can try something like:
SELECT * FROM table_1
WHERE CONCAT(',', `phone_number`, ',') REGEXP ',(123|432|645|234),'
OR CONCAT(',', `fax_number`, ',') REGEXP ',(123|432|645|234),';
Demo
Try creating an in-line table and join with it.
WITH telnostart(telnostart) AS (
SELECT '123'
UNION ALL SELECT '432'
UNION ALL SELECT '645'
UNION ALL SELECT '234'
)
SELECT
*
FROM your_search_table
JOIN telnostart ON (
LEFT(tel_number,3) = telnostart
OR LEFT(fax_number,3) = telnostart
you can use a case statement to add a flag column
select *
,case when left(phone_number,3) in (123,432,645,234) or left(fax_number,3) in (123,432,645,234) then 1 else 0 end as contact_check_flag
from table_name
As per your requirement, you can filter it or use it elsewhere.
SELECT * FROM table_1
WHERE `phone_number` REGEXP '^(123|432|645|234)'
OR `fax_number` REGEXP '^(123|432|645|234)';
But it won't be fast. (And no regular INDEX will help.)
If there phone numbers are spelled out like in the US: "123-456-7890", then you could use a FULLTEXT(phone_number, fax_number) index and
SELECT * FROM table_1
WHERE MATCH(phone_number, fax_number)
AGAINST('123 432 645 234');
This is likely to be much faster, but not as "general".
I am joining the tables Table_A and Table_B on columns Col_A and Col_B. Below are some test sample values.
Table_A, Col_A
USA1FullCover
USAMainland
USA2Islands
Table_B, Col_B
USA
USA1
USA2
When joining, I need to match the value 'USA' followed by a number exactly. For instance, the join result should look like this.
Col_A Col_B
USA1FullCover USA1
USAMainland USA
USA2Island USA2
I'm trying to achieve this in MySQL. I tried the rlike function. But the issue is that with rlike, I am not able to completely match them.
select
case when 'USA1FullCover' rlike 'USA' then 1 else 0 end;
#matches, but shouldn't
select
case when 'USA1FullCover' rlike 'USA1' then 1 else 0 end;
#matches, which is what I need/expect
select
case when 'USA1FullCover' rlike 'USA2' then 1 else 0 end;
#doesn't match, which is what I need/expect
My question is how can I fix the rlike so that the first case doesn't happen i.e. it doesn't match when there is no digit on the RHS? Or is it possible using regex? Taking a substring of the LHS doesn't help since we cannot really define the length beforehand.
Use
col_A regexp '^USA[0-9]{1}.+$'
to match USA followed by exactly one digit followed by any other characters.
To join only when such pattern exists
select a.*,b.*
from tableA a
join tableB b on
case when a.col_A regexp '^USA[0-9]{1}.+$' then substring(a.col_A,1,4) else '' end
= b.col_B
I am trying to write an mysql script to see if the first character is a number or not, this is what I go so far
select CASE WHEN user_id REGEXP '[0-9]+' then 1 else 0 end as user_id from table
it returns all my data as 1...the colum is a varchar, I will have user_id like this 1234 or this USER-484 or `ADMIN-464567' IS what I am trying to do possible?
So you want to display the user_id instead? Then put your regexp in the WHERE clause
select user_id from table
where
user_id REGEXP '^[0-9]+'
Also you're missing a ^ which means "at the beginning of the line"
Try this to check if starting letter/digit is a number:
select CASE WHEN user_id
REGEXP '^[0-9]+'
then 1 else 0 end as user_id
from table;
or
select CASE WHEN user_id
REGEXP '^\d+'
then 1 else 0 end as user_id
from table;
I am just wondering about your query arrangement. I thought it should be like this,
select user_id
from table
where REGEXP '^\d+' ;
But yours is actually working with the correct regex. :)
* SQLFIDDEL DEMO
Since you are missing ^ before [0-9] it matches numbers at any place in your field. By specifying ^[0-9] it means that it should BEGIN with a number. So try using '^[0-9]'
I have this SQL condition that is supposed to retrieve all rows that satisfy the given regexp condition:
country REGEXP ('^(USA|Italy|France)$')
However, I need to add a pattern for retrieving all blank country values. Currently I am using this condition
country REGEXP ('^(USA|Italy|France)$') OR country = ""
How can achieve the same effect without having to include the OR clause?
Thanks,
Erwin
This should work:
country REGEXP ('^(USA|Italy|France|)$')
However from a performance point of view, you may want to use the IN syntax
country IN ('USA','Italy','France', '')
The later should be faster as REGEXP can be quite slow.
There's no reason you can't use the $ (match end of string) to fill in your "empty subexpression" issue...
It looks a little weird but country REGEXP ('^(USA|Italy|France|$)$') will actually work
You could try:
country REGEXP ('^(USA|Italy|France|)$')
I just added another | after France, which should would basically tell it to also match ^$ which is the same as country = ''.
Update: since this method doesn't work, I would recommend you use this regex:
country REGEXP ('^(USA|Italy|France)$|^$')
Note that you can't use the regex: ^(USA|Italy|France|.{0})$ because it will complain that there is an empty sub expression. Although ^(USA|Italy|France)$|^.{0}$ would work.
Here are some examples of the return value of this regex:
select '' regexp '^(USA|Italy|France)$|^$'
> 1
select 'abc' regexp '^(USA|Italy|France)$|^$'
> 0
select 'France' regexp '^(USA|Italy|France)$|^$'
> 1
select ' ' regexp '^(USA|Italy|France)$|^$'
> 0
As you can see, it returns exactly what you want.
If you want to treat blank values the same (e.g. 0 spaces and 5 spaces both count as blank), you should use the regex:
country REGEXP ('^(USA|Italy|France|\s*)$')
This will cause the last row in the previous example to behave differently, i.e.:
select ' ' regexp '^(USA|Italy|France|\s*)$'
> 1
I need a MySQL query w/ Regex to tell me if my string's first character is a number from 0 to 9.
The following query returns '1', since the REGEXP matches. You can adapt it for your purposes:
SELECT '123 this starts with a digit' REGEXP '^[[:digit:]]';
You can use it in a SELECT like this:
SELECT * FROM tbl WHERE field REGEXP '^[[:digit:]]';
Use this:
SELECT 'a12' REGEXP '^[0-9]';
=> 0
SELECT '4ab' REGEXP '^[0-9]';
=> 1