MySQL regex style replace? - mysql

I have a column in a table that contains the path for a file, along the lines of:
/here/here2/something.jpg
I need to change every row to map it to:
/here3/something.jpg
Is there an easy way to do this? Thanks!

You don't need regex for this. Simple string functions can do the trick. You could use a query like this:
update test set path=concat('/here3/',
substring(path, length('/here/here2/') + 1))
where path like '/here/here2/%';
Here is a little test case to prove it works:
mysql> create table test (path varchar(64));
Query OK, 0 rows affected (0.02 sec)
mysql> insert into test (path) values
-> ('/here/here2/something.jpg'),
-> ('/here/here2/something-else.jpg'),
-> ('/here/here2/yet-another-something.jpg');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from test;
+---------------------------------------+
| path |
+---------------------------------------+
| /here/here2/something.jpg |
| /here/here2/something-else.jpg |
| /here/here2/yet-another-something.jpg |
+---------------------------------------+
3 rows in set (0.00 sec)
mysql> update test set path=concat('/here3/',
-> substring(path, length('/here/here2/') + 1))
-> where path like '/here/here2/%';
Query OK, 3 rows affected (0.00 sec)
Rows matched: 3 Changed: 3 Warnings: 0
mysql> select * from test;
+----------------------------------+
| path |
+----------------------------------+
| /here3/something.jpg |
| /here3/something-else.jpg |
| /here3/yet-another-something.jpg |
+----------------------------------+
3 rows in set (0.00 sec)

First of all, are you familiar with using PHP? You could write a script to get all of the entries from the database, and then change them using preg_replace(). Then you could use mysql to update the database.

Related

how to use mysql rewriter plugin?

I am trying to use mysql default rewrite plugin as per their instructions but it is giving me some grief. Simply put after activating rules the plugin is simply not working.
Below is my table and rules but simply queries are not reporting correct results. Can someone tell me what am i doing wrong?
Thanks.
mysql> select creditcard_number from mihir_test.creditcard_info where name = "mihir";
+-------------------+
| creditcard_number |
+-------------------+
| 1234567890123456 |
+-------------------+
1 row in set, 1 warning (0.00 sec)
mysql> insert into query_rewrite.rewrite_rules ( pattern, replacement) values ( 'select creditcard_number from mihir_test.creditcard_info where name = ?', 'select name, creditcart_number from mihir_test.creditcard_info where name = ?' );
Query OK, 1 row affected, 1 warning (0.01 sec)
mysql>
mysql> CALL query_rewrite.flush_rewrite_rules();
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> select creditcard_number from mihir_test.creditcard_info where name = "mihir"; +-------------------+
| creditcard_number |
+-------------------+
| 1234567890123456 |
+-------------------+
1 row in set, 1 warning (0.00 sec)
mysql>

How to SELECT JSON data stored in as text

I have to extract data from a MariaDB database where the owners have stored JSON data in varchar fields in the form:
[-100, -18.3, -10.1, 2.2, 5.8, ...]
I would like to be able to select individual entries from each of these JSON encoded text fields.
I have been reading about the many features of JSON support in MariaDB and I have looked at many examples of how data can be stored as JSON in text fields, but they all would require changes to how the data is inserted and/or the schema.
I cannot change the DB in any way. I have ReadOnly access.
The owners of the DB are currently using MariaDB 10.0, but I may be able to get them to upgrade to 10.1
In short, given the following (very simple example), how can I select the 2nd element in the ‘data’ field?
I assume using the JSON features is the way to go (given all the data is JSON), but is there another way? Performance isn't all that important.
MariaDB [mtest]> show columns from cal from mtest;
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| data | varchar(255) | YES | | NULL | |
+-------+--------------+------+-----+---------+-------+
1 row in set (0.00 sec)
MariaDB [mtest]> select * from cal;
+---------+
| data |
+---------+
| [10.1,12.0,16.78,18.9] |
+---------+
1 row in set (0.00 sec)
If you can upgrade to 10.1 (from MariaDB 10.1.9) via CONNECT can use JsonGet_Real function.
Try:
MariaDB [_]> SELECT VERSION();
+-----------------+
| VERSION() |
+-----------------+
| 10.1.14-MariaDB |
+-----------------+
1 row in set (0.00 sec)
MariaDB [_]> INSTALL SONAME 'ha_connect';
Query OK, 0 rows affected (0.01 sec)
MariaDB [_]> CREATE FUNCTION `jsonget_real` RETURNS REAL SONAME 'ha_connect.so';
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> DROP TABLE IF EXISTS `cal`;
Query OK, 0 rows affected, 1 warning (0.01 sec)
MariaDB [_]> CREATE TABLE IF NOT EXISTS `cal` (
-> `data` VARCHAR(255)
-> );
Query OK, 0 rows affected (0.00 sec)
MariaDB [_]> INSERT INTO `cal`
-> (`data`)
-> VALUES
-> ('[10.1,12.0,16.78,18.9]');
Query OK, 1 row affected (0.00 sec)
MariaDB [_]> SELECT `data` FROM `cal`;
+------------------------+
| data |
+------------------------+
| [10.1,12.0,16.78,18.9] |
+------------------------+
1 row in set (0.00 sec)
MariaDB [_]> SELECT `jsonget_real`(`data`, '[1]', 2) FROM `cal`;
+--------------------------------+
| jsonget_real(`data`, '[1]', 2) |
+--------------------------------+
| 12.00 |
+--------------------------------+
1 row in set (0.00 sec)

mysql-How to handle query search with \(backslash)?

i have inserted a column in mysql table with backslash(\)like user\12345.But when i see in the table values i am not seeing the forward slash(\). I am just seeing user12345.
When i query the just inserted row with backslash(\) with the below possible ways, i am always getting the zero records.
select * from users where user='user12345'
select * from users where user='user\12345'
select * from users where user='user\\12345'
when i copy paste the column value into notepad+ i could see there is a carriage retun in the end of every column values. user12345CRLF, as of now i can't update/remove the carriage return. but i want to fetch the records with carriage return. how can i do this?
I don't want to use like query, and i want to query for the exact username. how can i do this?
Backslash character \ is used for escape sequence. when you insert value like 'user\12345' then by default mysql server takes it as '\1' as escape character but it is not any special character so it discard the \ from the string.
Hitesh> update test set fname='user\344';
Query OK, 2 rows affected (0.06 sec)
Rows matched: 2 Changed: 2 Warnings: 0
Hitesh> select fname from test;
+---------+
| fname |
+---------+
| user344 |
| user344 |
+---------+
2 rows in set (0.00 sec)
If you want to store \ in string then you should use \
Hitesh> update test set fname='user\\344';
Query OK, 2 rows affected (0.03 sec)
Rows matched: 2 Changed: 2 Warnings: 0
Hitesh> select fname from test;
+----------+
| fname |
+----------+
| user\344 |
| user\344 |
+----------+
2 rows in set (0.00 sec)
if you don't want to set \ as escape character then you can enable the sql mode NO_BACKSLASH_ESCAPES.
Hitesh> SET sql_mode = 'NO_BACKSLASH_ESCAPES';
Query OK, 0 rows affected (0.00 sec)
Hitesh> update test set fname='user\344';
Query OK, 0 rows affected (0.00 sec)
Rows matched: 2 Changed: 0 Warnings: 0
Hitesh> select fname from test;
+----------+
| fname |
+----------+
| user\344 |
| user\344 |
+----------+
2 rows in set (0.00 sec)
for case of carriage return to end of column, in query you can find the string which ends with \r in where clause and select column you can skip the last character which is \r
Hitesh> set ##sql_mode='STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected (0.00 sec)
Hitesh> update test set fname='user344\r';
Query OK, 2 rows affected (0.17 sec)
Rows matched: 2 Changed: 2 Warnings: 0
Hitesh> select left( fname, CHAR_LENGTH(fname)-1) as fname from test where fname like '%\r';
+---------+
| fname |
+---------+
| user344 |
| user344 |
+---------+
2 rows in set (0.00 sec)
Thanks a ton #Hitesh,#Barmar again. I figured it out the query for exact match search. select * from users where user='user12345\r'

MySQL Update Text field using TRIM(TRAILING

I am trying to remove a carriage return in a MySQL Text type data field using the Update command. I've tried the following and when I export a record and view in a text editor (image attached) I still see this character? What should I use to remove this?
update fort_property_res SET property_information = TRIM(TRAILING '\n' FROM property_information
update fort_property_res SET property_information = TRIM(TRAILING '\r' FROM property_information
update fort_property_res SET property_information = TRIM(TRAILING '\t' FROM property_information
Harder than it seems as though it should be, yes? Here is a way that works. Perhaps not the best, but it can get you started.
I tried something using rlike and it did not work with my first try. It seems that it should have though. O well.
mysql> create table foo (pk int primary key, name varchar(8));
Query OK, 0 rows affected (0.62 sec)
mysql> insert into foo values (1, 'aaa\t');
Query OK, 1 row affected (0.18 sec)
mysql> select * from foo;
+----+------+
| pk | name |
+----+------+
| 1 | aaa |
+----+------+
1 row in set (0.00 sec)
mysql> update foo set name = substr(name,1,length(name)-1) where hex(name) like '%09';
Query OK, 1 row affected (0.23 sec)
Rows matched: 1 Changed: 1 Warnings: 0
mysql> select * from foo;
+----+------+
| pk | name |
+----+------+
| 1 | aaa |
+----+------+
1 row in set (0.00 sec)

inserting hex value into mysql

Can there any way to insert a hex value into MYSQL?
I also want to be able to retreive it in hex form.
For example, something like:
INSERT INTO table ( hexTag )
VALUES ( HEX(0x41) );
And if I do this, I want it to put an 'A' into the table
For that particular use case, you can either insert the hex value directly and it will be interpreted as a string, or use HEX() to input and UNHEX() to output
mysql> create table hexTable(pseudoHex varchar(50));
Query OK, 0 rows affected (0.01 sec)
mysql> insert into hexTable values (0x41);
Query OK, 1 row affected (0.00 sec)
mysql> select * from hexTable;
+-----------+
| pseudoHex |
+-----------+
| A |
+-----------+
1 row in set (0.00 sec)
mysql> select HEX(pseudoHex) from hexTable;
+----------------+
| HEX(pseudoHex) |
+----------------+
| 41 |
+----------------+
1 row in set (0.00 sec)
mysql> delete from hexTable;
Query OK, 1 row affected (0.00 sec)
mysql> insert into hexTable values (HEX('A'));
Query OK, 1 row affected (0.00 sec)
mysql> select UNHEX(pseudoHex) from hexTable;
+------------------+
| UNHEX(pseudoHex) |
+------------------+
| A |
+------------------+
1 row in set (0.00 sec)
mysql> select * from hexTable;
+-----------+
| pseudoHex |
+-----------+
| 41 |
+-----------+
1 row in set (0.00 sec)
See these links.