MYSQL variable IN clause [duplicate] - mysql

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.

Related

SHOW COLUMN escaping mechanism [duplicate]

This question already has answers here:
How should I escape characters inside this LIKE query?
(4 answers)
Closed last year.
I have a column called \'column. I am able to use it in SELECT statements just like any other column. However, when I try to perform SHOW COLUMNS FROM myTable LIKE '\\''column' I get no results. I observed that it works if I double escape the backslash: '\\\\''column'.
I tested this from MariaDB console, but I also observed the same behaviour in MySQL 8.
How does the escaping work? How should I properly escape the value so that I can fetch the column information?
DB Fiddle
From the mysql documentation
MySQL uses C escape syntax in strings (for example, \n to represent
the newline character). If you want a LIKE string to contain a literal
, you must double it. (Unless the NO_BACKSLASH_ESCAPES SQL mode is
enabled, in which case no escape character is used.) For example, to
search for \n, specify it as \\n. To search for \, specify it as \\\\;
this is because the backslashes are stripped once by the parser and
again when the pattern match is made, leaving a single backslash to be
matched against.

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

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."

Modify str in db [duplicate]

This question already has answers here:
MySQL search and replace some text in a field
(7 answers)
Closed 5 years ago.
I have a table, and in one colu,m I store comma separated strings, like:
book, table, lamp (New)
need to write query loop through all the strings and remove
(New)
I think I can do it by first running a query to fetch results containing (New), then using PHP's REGEX, remove (New) and then update the the same row with new string. However this sounds a bit convoluted. Isn't there a way to do this with a single query?
There is a replace string function in mysql: https://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace
UPDATE <table> SET <column> = REPLACE(<column>,'(NEW)','')
Try it with select first to see if the result is what you want (you may want to remove the trailing space if new is always preceded by it)

Is there any function in MySQL for Regex Replace? [duplicate]

This question already has answers here:
How to do a regular expression replace in MySQL?
(13 answers)
Closed 6 years ago.
I have a table which needs stores some name and i need to replace a few characters before comparing them with another string
For instance, My table data is
abc
ghi:dki
ioe dsa
i read a string from user, which is of the form abc, ghi-dki, ioe-dsa. ie, all blankspaces, multiples spaces and symbols are converted to a hyphon(-). Now i need to compare. something like
SELECT MYCOLUMN FROM MYTABLE WHERE {Converted MYCOLUMN} = 'ghi-dki'
Can someone help me for figuring out which MySQL function can do it?
You can't do a regex replace in MySQL, but you can do a match.
SELECT mycolumn FROM tablename WHERE mycolumn REGEXP Replace('ghi-dki', '-', '^[\s:_-]*$');
Note: I didn't completely fill out the symbols character set, you'll have to add whatever you're using.

Retrving data from MySql with case sensitivity [duplicate]

This question already has answers here:
How can I make SQL case sensitive string comparison on MySQL?
(12 answers)
Closed 8 years ago.
I have a Login table in MySql database . . In table there is a column by cname and one of the value is 'Raghu'. My question is when i write the query as
Select *from Login where cname='raghu';
Then it is retrieving the record which contains 'Raghu' . I want it to retrieve according to case . How can I retrieve with case sensitively, values in the data of tables.
Use: 10.1.7.7. The BINARY Operator
The BINARY operator casts the string following it to a binary string. This is an easy way to force a comparison to be done byte by byte rather than character by character.
Select * from Login where binary cname='raghu';
SELECT * from Login WHERE STRCMP(cname,'Raghu')=0;
Can you try this, you can use LOWER FUNCTION in either column name LOWER('cname') or in value LOWER('raghu');
Select *from Login where LOWER(`cname`) = 'raghu';