Mysql select query to get FIND_IN_SET [duplicate] - mysql

This question already has answers here:
MySQL find_in_set with multiple search string
(7 answers)
Closed 9 years ago.
The following query works good.
SELECT FIND_IN_SET('b','a,b,c,d');
// output -> 2
I need to fetch the record having multiple options. Just for an example,
SELECT FIND_IN_SET('b,c','a,b,c,d');
// output -> ??????????
Please how do i get the record with multiple selection option "b,c" in "a,b,c,d".

mysql function find_in_set can search only for one string in a set of strings.
the second its not a string in set of strings
take a look here
edit:
to change the mode
This can be done in two ways...
1- Open your "my.ini" file within the MySQL installation directory, and look for the text "sql-mode".
Find:
Code:
Set the SQL mode to strict sql-mode="STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Replace with:
Code:
Set the SQL mode to strict sql-mode="NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION"
Or
2- You can run an SQL query within your database management tool, such as phpMyAdmin:
Code:
SET ##global.sql_mode= '';

Related

Can I add values with conditions in mySQL [duplicate]

This question already has answers here:
MySQL You are using safe update mode and you tried to update a table without a WHERE
(2 answers)
Closed 2 years ago.
I'm trying to add a numeric value to a column whose "AuthDate" is "06/05/20" and "Time" is "1P -2P".
I ran the following query:
update main
set calls = 2072
where authdate = '06/05/20'
and time = '1P - 2P';
*main = table name
calls = column name
but I get error code 1175 You are using safe update mode
Does anyone know why I get the error?
Your database or session has option sql_safe_updates enabled. Amongst other things, this forbids update that do not have a where clause, or that have a where clause whose columns are not keys (are not indexed). Even if there is an index, but the optimize chooses not to use it, the query is also aborted.
This is more of an option that prevents mistakes from beginners. If you really know what you are doing, you can disable the option at session level:
set sql_safe_updates = 0;
Side note: I am suspicious about condition authdate = '06/05/20':
if autdate is of a date-like datatype, then you should give it a legitimate date string, like: authdate = '2020-06-05' (or '2020-05-06'?)
if it's a string then... consider storing it as a date; storing dates as string is bad practice for many reasons, and should be avoided

Getting result set from non-sql query [duplicate]

This question already has an answer here:
How to get Description of MySQL Table in GoLang
(1 answer)
Closed 3 years ago.
How do I retrieve data sets from non-standard MySQL "show" statement using golang? For example, "show tables", "show variables", "show engine innodb status". etc.
I cannot find any information to retrieve the result set from mysql "show" statement in Golang. Using either database/sql package or sqlx package is fine.
Got it! Here is the code. It worked!
var r1, r2, r3 string
row := db.QueryRow("show engine innodb status")
err = row.Scan(&r1, &r2, &r3)

how to use mysql lock/unlock [duplicate]

This question already has answers here:
MySQL wait_timeout Variable - GLOBAL vs SESSION
(3 answers)
Differences between SET var = 'abc' and SET GLOBAL var = 'abc'?
(1 answer)
Closed 3 years ago.
I have two questions about MySQL usage:
I installed mysql5.7 by default options.
then I connect to MySQL by MySQL query browser:
1) if I run "SET autocommit='OFF'", then I use "show variables like 'autocommit';" to check the value, but I still get the result:
autocommit='ON'.
Only if I run "SET global autocommit='OFF';" then I can see the value has been changed, why?
2) with autocommit='ON' , I want to lock a table by:
lock tables xxx write
but I can still query this table from another session.
why?

SET mysql variable with list of mails [duplicate]

This question already has answers here:
How to pass a variable to a IN clause?
(4 answers)
Closed 5 years ago.
I need to define a list of variables to use in multiple MySQL queries.
The variable will be a list of mails. I have tried to define it in different ways but it always gives me error in the construction.
SET #listamails='mail1#gmail.com,mail2#gmail.com';
Select * from user WHERE mail IN (#listamails);
Any ideas?
Thank you
You cannot pass in a list to IN using a single variable. The simplest solution in MySQL is find_in_set():
Select u.*
from user u
where find_in_set(mail, #listamails) > 0;
However, this cannot take advantage of an index. For that, you might want to use dynamic SQL.
Try this
SET #listamails='mail1#gmail.com','mail2#gmail.com';
Select * from user WHERE mail IN (#listamails);

What is the correct syntax for a Regex find-and-replace using REGEXP_REPLACE in MariaDB?

I need to run a regex find-and-replace against a column named message in a MySQL table named post.
My database is running MariaDB 10.
According to the docs, MariaDB 10 has a new REGEXP_REPLACE function designed to do exactly this, but I can't seem to figure out the actual syntax.
It will affect 280,000 rows, so ideally there's also a way to limit it to only changing one specific row at a time while I'm testing it, or simply doing a SELECT rather than an UPDATE until I'm sure it does what I want.
The regex I want to run:
\[quote\sauthor=(.+)\slink=[^\]]+]
The replacement string:
[quote="$1"]
The following was what I tried, but it just throws a SQL error:
UPDATE post SET message = REGEXP_REPLACE(message, '\[quote\sauthor=(.+)\slink=[^\]]+]', '[quote="$1"]') WHERE post_id = 12
In this case, the original message was:
[quote author=Jon_doe link=board=2;threadid=125;start=40#msg1206 date=1065088] and the end result should be [quote="Jon_doe"]
What is the proper syntax to make this REGEXP_REPLACE work?
You have to do a lot of escaping here:
REGEXP_REPLACE(message, "\\[quote\\sauthor=(.+)\\slink=[^\\]]+]", "\\[quote=\"\\1\"\\]")
Please note that you have to reference the Group by \\1