MySQL: fix data inserted inside quotes in DB - mysql

I need to fix some (bad) inserted data in DB, I had data inserted with single qoutes like: ('love')
I want to move this to (love), without effect on something like (we're),
info table (structure)
id text
1 'love'
2 'man'
3 we're..
4 John
5 'Sarah'
I want to fix "info" table to be:
info table (structure)
id text
1 love
2 man
3 we're..
4 John
5 Sarah
I can select this data using:
select * from info where text like "'%'"
thanks,

try this untested query:
update info set text = replace(text,"'","") where text like "'%'"

You can use this statement to update all the rows of the table.
update Table_Name
set ColumnName= REPLACE(ColumnName,'''','')
where ColumnName LIKE ('''%''')

Related

MySQL strip ' on where queries

I have a large table which contains, or not, records that have ' tags like (martin's, lay's, martins, lays, so on).
Actually to search the client can be write exactly text, for example: martin's, to search all records that contains "martin's" but it is complicate, then, I need the client can to search by "martins" or "martin's".
This is a simple example:
A mysql table like:
ID | Title
---------------
1 lays
2 lay's
3 some text
4 other text
5 martin's
I need a sql query to search by lays or lay's and both need show me a Result like:
ID | Title
---------------
1 lays
2 lay's
I'm tried with many post solutions but I cant do that :-(
Appreciate any help.
Just remove the single quote:
select t.*
from t
where replace(t.title, '''', '') = 'lays';
To search if the word contains:
select t.*
from t
where replace(t.title, '''', '') LIKE '%lays%';

SQL query if data match in string from column 2 alter column 22

I have a table with 22 columns.
The particular columns interesting are 2 and 22.
Column 2 has text URLs in each row. I want to to match if URL string contains "this text" then I want to alter column 22 to set number "1" instead of the deafult 0.
I want to do this check for all 600 000 rows in this table. Any idea how to form the query?
A simple Update with like should do:
update your_table
set col_22 = 1
where col_2 like '%this text%';
If you want to make this update only on the rows where column 22 is set to 0 and column 2 contains the text you're looking for, then add another filter:
update your_table
set col_22 = 1
where col_2 like '%this text%'
and col_22 = 0;

Patern maching like Select Query

I have a table that contains this kind a structure of a column, how can I make select only from character 4 to 6 to ignore other character that are outside this boundary , I tried LIKE'%544%', RegExp. ect. ??
1 000544001
2 000054400
3 000544010
4 000344010
5 000544011
One way is to use substr():
where substr(col, 4, 3) = '544'
another is to use like:
where col like '___544%'
you can also use mid(col,start,length) statement
like this
select column1 from table1 where mid(column1,4,3);

Find emails in the same table that having %40 in MySQL

I have a table with two columns - id, email
What query can I run to show only single emails having %40 ?
e.g. my table is
id email
-------------
1 stevemartin140%40gmail.com
2 stevemartin141%40gmail.com
3 stevemartin140#gmail.com
4 stevemartin141#gmail.com
5 stevemartin148%40gmail.com
6 andymartin%40ymail.com
So result will be:
id email
-------------
5 stevemartin148%40gmail.com
6 andymartin%40ymail.com
i would like to find out & replace those emails with #
so my final output will be:
id email
-------------
1 stevemartin140%40gmail.com
2 stevemartin141%40gmail.com
3 stevemartin140#gmail.com
4 stevemartin141#gmail.com
5 stevemartin148#gmail.com
6 andymartin#ymail.com
Thanks in advance
If you want to replace them, then:
UPDATE t SET email=REPLACE(email, '%40', '#')
-in MySQL.
Maybe not the must performance one but to find duplicate...
SELECT * FROM table GROUP BY REPLACE(email, '%40', '#') HAVING COUNT(*) < 2 AND email LIKE '%\%40%';
MySQL compliant
http://www.sqlfiddle.com/#!2/6808c/3/0
You should use like '%\%40%' in your where condition to escape the wildcard, like this:
select * from t1 where email like '%\%40%';
here is the SqlFiddle

Having a number in

Can someone give me a query that will return as a result rows ID 1 & 3?
ID Name Hidden
1 Mika 1,4,2
2 Loca 0
3 Nosta 4
4 Like 2
Something like this
SELECT * FROM table WHERE Hidden HAVING(4)
SELECT * FROM table WHERE FIND_IN_SET('4',Hidden);
docs for FIND_IN_SET
SELECT * FROM table WHERE CONCAT(',',Hidden,',') LIKE '%,4,%'
or you can avoid using LIKE like this
SELECT * FROM table WHERE INSTR(CONCAT(',',Hidden,','), ',4,') > 0
this will not get things like 40, 14, etc, but you need to make sure there are no spaces in the Hidden field (eg, 1, 4, 5 or update the concat and LIKE function accordingly.
SELECT * FROM table WHERE Hidden LIKE '%4%'
the % are wildcards.
Full Text Search might be a reasonable solution for this as long as you use the correct word breaks.
Either go with Full Text Search, as suggested, or
Spin the Hidden values off into a separate table, with the ID of current row.
Eg, Mika would have three entries in this table
ID = 1, Hidden =1
ID = 1, Hidden =4
ID = 1, Hidden =2
Then you could return results against this spin off table.
You may also want to consider normalizing the table and storing these "hidden" values in a separate table with an index on the apropriate column. Depending on the number of rows you have that would be much faster:
ID Hidden
1 1
1 4
1 2
3 4
4 2
and:
SELECT DISTINCT table.* FROM table, hidden_table WHERE table.ID = hidden_table.ID AND hidden_table.hidden = 4