Why my sql REGEXP_REPLACE is not changing all matches? - mysql

I'm using mariadb with this code:
UPDATE files SET file_source = REGEXP_REPLACE (file_source, 's:[1-9][1-9][1-9]|s:[1-9][1-9]|s:[1-9]', "s:12") WHERE type = 2;
I want to change all the columns where s: is followed by up to 3 numbers, and replace it with s:12
But when I run this it says:
Query OK, 10012 rows affected (0.118 sec)
Rows matched: 10375 Changed: 10012 Warnings: 0
I don't understand why? How I can see what didn't change and why? Is there a error in my code?
Also if I run this twice, it will say 0 changed, but from my understanding, this code should change everything again to s:12 even if it's already s:12or am I wrong?

This works fine
Update Table1 SET file_source =
REGEXP_REPLACE (file_source, 's:[0-9]?[0-9]?[0-9]', 's:12') Where TYPE = 2;
With s:1
s:31
and s:111
See https://dbfiddle.uk/?rdbms=mariadb_10.4&fiddle=506350e8fa09b7270fb5e8b46c2e2f6f

Related

What is note-level warning in MySQL?

Okay, I understand what are errors and warnings in the context of MySQL. But what's the need of note-level warning? I have already searched the MySQL documentation but didn't find anything relevant. It would be better if someone could shed some light on the what are they and why they are useful.
mysql> create database if not exists city;
Query OK, 1 row affected, 1 warning (0.00 sec)
mysql> show warnings
-> ;
+-------+------+------------------------------------------------+
| Level | Code | Message |
+-------+------+------------------------------------------------+
| Note | 1007 | Can't create database 'city'; database exists |
+-------+------+------------------------------------------------+
1 row in set (0.00 sec)
I've always considered Note to be like an "FYI": something happened, or didn't, that may be of interest. The closest definition I can find in the docs is:
... events that do not affect the integrity of the reload operation
which is from the sql_notes server variable, one perhaps not often used outside of mysqldump.
Trawling through the MySQL source code, looks like Sql_Condition::SL_NOTE annotates warnings of this level. There are a few, but they are mostly as you'd expect for non-impactful information:
Event already exists
Table already exists
Query '%s' rewritten to '%s' by a query rewrite plugin
Password set
Sadly, I would have expected the code docblock to give a little more information about them, but it doesn't:
class Sql_condition {
public:
/**
Enumeration value describing the severity of the condition.
*/
enum enum_severity_level { SL_NOTE, SL_WARNING, SL_ERROR, SEVERITY_END };
This might warrant a documentation bug report to MySQL team.
Interestingly, MariaDB has this to say:
A note is different to a warning in that it only appears if the sql_notes variable is set to 1 (the default), and is not converted to an error if strict mode is enabled.
My takeaway from that, in Maria and possibly by extension MySQL: notes are warnings, but ones that can be ignored because no data-loss or side-effect is described.

Unable to upload image file in MySQL

I used the following code for uploading an image file into my database from an answer in stack overflow, but it is not getting updated. Please help.
mysql> update S516 set photo = LOAD_FILE('/home/rsreekumar/db/java16/photos/13134.PNG') where roll_no = "AM.EN.U4CSE13134";
Query OK, 0 rows affected (0.00 sec)
Rows matched: 1 Changed: 0 Warnings: 0
My question is same. but not working.
Can you check?
mysql user has permissions to read file
where condition is correct
column photo is blob
you have permissions to use LOAD_FILE
file size less than max_allowed_packet bytes
if the secure_file_priv system variable is set to a nonempty directory name, the file to be loaded must be located in that directory
is AppArmor protection running (and blocking)?

MySQL - UPDATE part of a string in a column

I am trying to replace some text in a column - text that I have confirmed exists. When I run the query:
SELECT Body FROM dbname.tcontent
where body like '%http://local.website.org%'
and display=1
and approved=1;
it returns 359 records. So, I was expecting 359 records to be changed when I ran this query:
update dbname.tcontent
set Body = replace(Body, 'http://local.website.org', '/foldername')
where instr(Body, 'http://local.website.org') > 0
and display=1
and approved=1;
However, the query found 359 matches, but updated no columns. The message reads: "0 row(s) affected Rows matched: 359 Changed: 0 Warnings: 0. I'm pretty sure I've got the SQL correct as I've confirmed it from at least three sources. I have read that the 359 "matches" are based on records matching the WHERE clause, and so I believe the problem is with that second line:
set Body = replace(Body, 'http://local.website.org', '/foldername')
even though the SQL is correct. I've unchecked "Safe Updates" in my preferences just to be sure, and still, no rows are being updated.
I still think that maybe the slashes and/or dots in the text I am replacing are tripping up the replace method despite an example of doing what I want to do here.
Thanks in advance for any assistance!
For whatever reason, I had to break it down into two statements. First Update statement replaced http:// with nothing, and the second replaced the remainder of the URL:
update database.tcontent set Body = replace(Body, 'http://', '') where Body like '%http://local.website.org%';
update database.tcontent set Body = replace(Body, 'local.website.org', '/foldername') where Body like '%local.website.org%';
Try this query instead:
UPDATE `dbname`.`tcontent`
SET `body`= REPLACE(`body`, 'FindThisText', 'ReplaceWithThisText')
WHERE `body` like '%FindThisText%' AND `display`=1 and `approved`=1;
Always backup your table before running such UPDATE queries.
Does this not work?
Set Body = 'ReplaceWithThisText' ?

Show exact timing on SQL query

When I do the following statement, I only get two decimal places on the output:
> select * from table;
98 rows in set (0.00 sec)
How would I get more precision on this (without using SHOW PROFILES). For example, it would be nice to see:
> select * from table;
98 rows in set (0.001837884 sec)
Hey man what you want is a bit hardcore. You need to customize the nice_time() on mysql core, then you can set the decimal places you wanted.
There is a another question like this on stackoverflow, you can access this link to further information:
How do I see high-precision query times in mysql command line?

Codeigniter db->update() VS MySQL native UPDATE Affected rows: 0

Using MySQL alone - If I make a basic update to a table like this:
UPDATE `SOMETABLE` SET `NAME` = 'John' WHERE `ID` = 1;
And the value of NAME = 'John' was already 'John' - in other-words - nothing is new, nothing to update. MySQL returns "Affected rows: 0 (Query took 0.0007 sec)"
If I make the same call - now using CodeIgniter - and then retrieve the affected rows like this:
$data = array(
'NAME' => 'John'
);
$this->db->where('ID', 1);
$this->db->update('SOMETABLE', $data);
$affect = $this->db->affected_rows();
echo $affect; // $affect echos 1
$affect ends up equaling 1.
I haven't got a problem with this - I just expected that if there was nothing to update - that codeigniter would behave the same way as MySQL and not edit something that does not need to be updated, and return 0 for affected_rows().
Have I got this wrong some way?
Is codeigniter overwriting 'John'? or not?
Try getting the query that CodeIgniter is running using the following code:
$this->db->last_query();
Also post the query you are using to interact with MySQL, just to confirm that the exact same query is being run.
CodeIgniter does have a hack for MySQL that adjusts the reporting of affected rows, however I was under the impression it was only for DELETE queries. If you look at system/database/drivers/mysql/mysql_driver.php or system/database/drivers/mysqli/mysqli_driver.php (whichever driver you are using and look at the variable var $delete_hack = TRUE;. Adjusting that might impact your result, could be worth a try?