Using regular expression to search and replace in MySQL - mysql

I am trying to write a query in MySQL to update a field by adding a new text string to the start of its existing text, creating a new line, and then adding the original text.
I have looked into regexp, but am unsure as to whether I am using it properly, as my query doesn't seem to work with the match.
The query I am using is as follows:
UPDATE table_name SET field = REPLACE(field, 'REGEXP ^', ''new_text' REGEXP \n field');
The ^ represents the beginning of the string(start of the text) that I wish to update in the field.
The replacing text consists of the new_text string, the new line (\n) and the original field.
I am relatively new to MySQL, so any insight into the structure of this query and whether it is possible to implement would be greatly appreciated.

You don't need regular expressions for this.
If you have a table of people
+----+-------+
| id | name |
+----+-------+
| 1 | Bob |
| 2 | Frank |
+----+-------+
Doing
UPDATE people SET name = CONCAT('Mr. ', name) WHERE id = 1;
Will give you
+----+---------+
| id | name |
+----+---------+
| 1 | Mr. Bob |
| 2 | Frank |
+----+---------+

Related

How to update specific value without updating a whole value in MySQL

I have a table like this
+-----+------------------+
| id | name |
+-----+------------------+
| 1 | John;Black;Mike |
+-----+------------------+
| 2 | White;Mike;John |
+-----+------------------+
| 3 | Jacob;Mike |
+-----+------------------+
| 4 | Will;Mason;Mike |
+-----+------------------+
as result of
SELECT * FROM people WHERE name LIKE '%Mike%';
Is there any query on how to update specific name Mike to Michael without updating a whole value. like John;Black;Mike to John,Black,Michael in all rows automatically.
You could use replace
update people
set name = replace( name, 'Mike', 'Michael')
where name LIKE '%Mike%';
anyway you should avoid storing comma separated value .. you should think to a proper normalized table for this data ..

MySQL query to SELECT rows with LIKE and create new column containing the matched string

I need some help with a MySQL query I am struggling for some time now.
So, I am trying to create a MySQL query to SELECT rows from a table that match a specific string like app.
My table is like this:
+-----+--------------+
| id | name |
+-----+--------------+
| 1 | Green Apple |
| 2 | Big Orange |
| 3 | application |
+-----+--------------+
I can find all rows that contain app string with SELECT and LIKE.
However, I also want to create new column that contains the string from name column which matches app and keep the database case sensitive format, i.e. with app as a match phrase the new column will contain App and app entires according to the string format in name.
My query so far goes like this:
SELECT *, 'what_to_put?' as new_column FROM table WHERE name LIKE '%".$app."%'
The desired output is the following:
+-----+--------------+-------------+
| id | name | new_column |
+-----+--------------+-------------+
| 1 | Green Apple | App |
| 2 | application | app |
+-----+--------------+-------------+
Any idea how to achieve this?
Without a separate regex library, you'll need to use the built-in string functions to find the location of the match, and then extract the matching sub-string:
SELECT
id,
name,
substring(name, locate('app', name), length('app')) as new_column
FROM yourTable
WHERE name LIKE '%app%'
Which gives the results:
+----+-------------+------------+
| id | name | new_column |
+----+-------------+------------+
| 1 | Green Apple | App |
| 3 | application | app |
+----+-------------+------------+
Sql Fiddle Here

Match Regex in MySQL for repeated word in one column

I'm having a query problem. I use mysql as DB.
I want to use a REGEX to match the result I expected
and The Table is
table A
----------------------------------
| ID | Description |
----------------------------------
| 1 | new 2 new 2 new 2 new |
| 2 | new 2 new 2 new |
| 3 | new 2 |
| 4 | 2 new 2new |
The Result I expected
---------------------------------
| ID | Description |
---------------------------------
| 2 | new 2 new 2 new |
| 4 | 2 new 2new |
The Query I've tried so far:
SELECT * FROM a WHERE (description REGEXP '([^2][^0..9]])2( [^2][^0..9])([^2][^0..9]])2( [^2][^0..9])')
http://sqlfiddle.com/#!2/7d712/2
Could anyone help me to solve this :(?
Your regex isn't doing what you think it does (although I can't quite guess what you think it does...)
A translation of part of your regex:
([^2][^0..9]])2
means:
( # Start a group
[^2] # Match one character except "2"
[^0..9] # Match one character except "0", "." or "9"
] # Match "]"
) # End of group
2 # Match "2"
As #Tim Pietzcker pointed out, your regular expression does not do what you may think it does. If I understand correctly, I believe you are looking for the following regular expression. This returns ID 2 and 4 respectively.
^[^2]*2[^2]*2[^2]*$
Your SQL query would be:
SELECT * FROM a WHERE (description REGEXP '^[^2]*2[^2]*2[^2]*$')
SQL Fiddle

Using MySQL how can I create a list of all words in a set of strings?

Let's say I've got a table like this:
| RowID | LongString |
----------------------------------------
| 1 | This is a really long string |
| 2 | This is a shorter string |
How can I get a list of distinct words used in all the rows such as below:
| Result: |
-----------
| This |
| is |
| a |
| really |
| long |
| string |
| shorter |
From MySQL docs:
MySQL does not include a function to split a delimited string. Although separated data would normally be split into separate fields within a relation data, spliting such can be useful either during initial data load/validation or where such data is held in a text field.
So there is not ready-to-go solution. If I were you, I would split such string after fetching it from DB (it is easy to do in PHP, Java C# and so on).
Howewer on this site someone has wrote procedure for such task. Check it out. It is in comments section below.

How to select the first letter of each word from a table cell in MySQL?

How can I select the first letter of each word in MySQL using a query?
So this table
+----+----------------------------+
| id | str |
+----+----------------------------+
| 1 | Hello my name is MCEmperor |
| 2 | How are you doing? |
+----+----------------------------+
would return
+----+----------------------------+
| id | str |
+----+----------------------------+
| 1 | HmniM |
| 2 | Hayd |
+----+----------------------------+
I guess it's something with SUBSTRING and LOCATE and maybe I need a loop (to find all spaces or something)...
Is it possible within a query? How should I do that?
Maybe you could simply split by space? Use this stored proc : http://forums.mysql.com/read.php?60,78776,148332#msg-148332
You can then retrieve the first letters of each word and use GROUP_CONCAT in a GROUP BY Id to put the letters back into one line per initial text.
What you're looking for is a WHERE clause that matches only part of the data in the cell. You can do that like so:
SELECT str
from (table name)
WHERE str LIKE 'H%'