Find ID in string of IDs separated with comma [duplicate] - mysql

This question already has answers here:
MySQL query finding values in a comma separated string
(11 answers)
Closed 1 year ago.
I would appreciate help with one regex.
I have a string of ids example "123,55,68,890,456,333,168"
How should the regex look like to find a specific id -- example 68? Bear in mind that 168 shouldn't be returned.
There are four cases where the id could be positioned:
x (only one id in the list/string)
,x, (somewhere in the middle of the string)
x, (at the beginning of the string)
,x (at the end of the string)
I would use this regex as part of the SQL query.
Thanks in advance

/(?:^|,)number(?:$|,)/ should do the trick.
(?:) is a non-capturing group. It looks for the value in the parentheses after the :, but doesn't include it in your result.
So this regular expressions says "Find number that is at the beginning or preceded by a comma, and at the end or followed by a comma."

Related

Mysql regexp to get right table names from database schema [duplicate]

This question already has an answer here:
How to match a fixed string ending with a number using regex [duplicate]
(1 answer)
Closed 2 years ago.
I'm trying to select, from mysql, some specific tables that matches a pattern.
The specific pattern I'm looking for is like 'my_table_number'. For example, my_table_436814 or my_table_35413. The thing is that there are other tables that looks like my_table_old_14353 or my_table_351434_times. I just want to filter out the ones that are my_table_number.
I have tried different patterns, but failed to get what I really need.
The most closest approach was with this:
select table_name from
information_schema.columns ca
where ca.table_name REGEXP '[0-9]$'
How can I achieve what I'm looking for?
Use
REGEXP '^my_table_[0-9]+$'
See proof
NODE
EXPLANATION
^
the beginning of the string
my_table_
'my_table_'
[0-9]+
any character of: '0' to '9' (1 or more times (matching the most amount possible))
$
before an optional \n, and the end of the string

How does "not regexp '^[aeiou].*[aeiou]$' " work? [duplicate]

This question already has an answer here:
Reference - What does this regex mean?
(1 answer)
Closed 2 years ago.
The question is to 'query the list of CITY names from STATION that either do not start with vowels or do not end with vowels.' But the not here should work as "and" instead of "or"?
The problem is stated as "NOT(starting with a vowel) OR NOT(ending with a vowel)". The sql expression is saying "NOT(starting with a vowel AND ending with a vowel)", which is equivalent: https://en.wikipedia.org/wiki/De_Morgan%27s_laws
...except that the sql expression will produce the wrong result for city names that are just a single vowel character, because the regex will only match values when there are at least two characters.
You regex should be
(^[aeiou])|([aeiou]$)
Right now regex will only match if it start and ends with a vowel, while you want to say, start with vowel and it doesn't matter what is after, or it doesn't matter what is at the begining as long as it ends with a vowel.

Find position of word in string [duplicate]

This question already has answers here:
Locate text position, extract text and insert in new column in MySQL
(2 answers)
Closed 2 years ago.
I have string
good morning what a lovely day today
I need to find the position of 'lovely'. Query should return the result 5.
You can use find_in_set() for this:
select find_in_set('lovely', replace('good morning what a lovely day today', ' ', ','))
Yields:
5
From the documentation:
FIND_IN_SET(str,strlist)
Returns a value in the range of 1 to N if the string str is in the string list strlist consisting of N substrings. A string list is a string composed of substrings separated by , characters.
So basically the logic is to replace space characters with commas, and then use that function.

MYSQL variable IN clause [duplicate]

This question already has answers here:
MySQL variable format for a "NOT IN" list of values
(3 answers)
Closed 8 years ago.
In filtering out some spam, I have two MYSQL statements in one file,
SET #valid_users := '"admin", "jrock", "kmicka", "First Last"'; //etc
Followed by a SELECT like this
SELECT /*stuff*/ /*WHERE*/ /*filters*/ AND User.user_name NOT IN (#valid_users)
When I do this, it acts as if #valid_users is an empty string. (Returns all results). But if I change the clause to NOT IN ('admin', 'jrock', etc) then it works as it should.
Why would a variable in the NOT IN filter not work?
You'll want to take a look at MySQL's find_in_set() function:
SELECT
*
FROM
your_table
WHERE
NOT FIND_IN_SET(User.user_name, #valid_users);
For this to work, the comma-separated list shouldn't contain quotes (unless your usernames actually contain quotes) and should not be padded with spaces:
SET #valid_users := 'admin,jrock,kmicka,First Last';
SqlFiddle Example
To directly answer your question regarding "why would a variable in the NOT IN filter work", it's because #valid_users is being treated as a string and when you pass it to IN(), it's being treated as a single string (i.e. not a set/list). With FIND_IN_SET(), it treats the string in #valid_users as a comma-separated set/list and uses it accordingly.

mySql - find non-Ascii character in html [duplicate]

This question already has answers here:
How can I find non-ASCII characters in MySQL?
(10 answers)
Closed 9 years ago.
I'm trying to find all non-asci characters I have in my DB in a specific table and column. In that column are stored Html description, and in some of them I've exotic or non-existing characters (for example: Hà¶ganà¤s ).
I'm triyng to match them with this query:
SELECT * FROM project_version WHERE description REGEXP '[^()\x00-\xFF\,\.-\<\>="\' /:;&=]'
But I think I'm missing something, cause it returns all of my records. Does anyone any advice?
Thanks in advance
Try moving hyphen to start or end otherwise it needs to be escaped also ^ will be treated as literal ^ in character class:
SELECT * FROM project_version WHERE description REGEXP '[()\x00-\x7F,.<>="\' /:;&=-]'