How to update specific value without updating a whole value in MySQL - 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 ..

Related

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

How to replace all values of a MysQL field like a regular expression

I want to add some value in a mysql field (please note i don't want to append). For example my data looks like:
+------------+---------------+
| col1 | value |
+------------+---------------+
| DCM4CHEE01 | "aaaa","bbbb" |
| DCM4CHEE01 | "xxxx","yyyy" |
+------------+---------------+
I want to add a piece to string to the value field (not append) like:
+------------+-------------------+
| col1 | value |
+------------+-------------------+
| DCM4CHEE01 | "aaaa","bbbb-mgr" |
| DCM4CHEE01 | "xxxx","yyyy-mgr" |
+------------+-------------------+
I want to replace the last occurance of " with -mgr". How can I achieve this by running one single mysql query?
You can try like this:
select concat(left('"aaaa","bbbb"',length('"aaaa","bbbb"') -1),'-mgr"')
DEMO
I am aware that it is not using Replace but I think this is a better way of achieving what you are asking. If you want to update all column then it will be like
UPDATE mytable
SET value =concat(left(value ,length(value) -1),'-mgr"')
use INSERT
INSERT('"aaaa","bbbb"', length('"aaaa","bbbb"'), 5, '-mgr"')

"where" or "like" clause is better for using index

I have a table with columns of type SET e.g SET('abc','def','ghi') wich store the data like "abc,ghi" and I index these columns. So when I want to find "def" or "ghi" I have to use LIKE "%def%" but I read about "%" that if you use it as first character mysql doesn't use index for search. Waht should I do? should I change the type to enum and store each value in separate row with an ID like this:
+---------+
| column |
+---------+
| abc |
| abc,ghi |
| abc,def |
| ghi,def |
+---------+
change to:
+----+--------+
| ID | column |
+----+--------+
| 1 | abc |
| 2 | abc |
| 2 | ghi |
| 3 | abc |
| 3 | def |
| 4 | ghi |
| 4 | def |
+-------------+
or is there any thing to manipulate index to store each word separately?
The correct function to find an item in a set is FIND_IN_SET. Sets are stored as bit maps, not as strings, and FIND_IN_SET will not have to convert it to a string before matching like LIKE would. But it still won't be able to use an index.
Your second schema is the proper way to normalize the data. You can put an index on the column column, and queries that look for a value will be efficient. Whether to use an ENUM or VARCHAR for this column is a subject of intense debate within the database community.

check if the value in any of these columns mysql

I got table called 'pet' have the following values
id | name | nickname | dateofborn |
1 | test | jhon | 2009 |
2 | test2| test | 2010 |
3 | mike | NULL | 2010 |
3 | jhon | testor | 2011 |
I want to select all of columns that contain 'test' value either in name column or nickname column so i have this query didn't actually work for me:
SELECT * FROM pet WHERE name='test' OR nickname= 'test'
note that I exactly want test value not testor.
The query is right, I think you have some problems with your Table.
But try like below:
USE [yourDB es: animals];
SELECT * FROM [yourDB].pet WHERE (name='test' OR nickname='test');
try this.
SELECT * FROM pet WHERE name like 'test%' OR nickname like 'test'

Joining Two Tables in MySQL with diff column name

I'm not very good at joining tables in mysql and I'm still learning,
So I wanted to ask, when joining two tables....
I have 2 tables
So for the first table I want to join the 2 of its columns (id & path) on the second table.
But on the second table there's no column name id and path, there is a column name pathid & value. The field of the pathid column is the same as the id.
it looks like this.
first table
| id | path |
---------------------
| 1 | country/usa |
| 2 | country/jpn |
| 3 | country/kor |
second table
| pathid | value |
-------------------
| 3 | 500 |
| 1 | 10000 |
| 2 | 2000 |
So on the first table, it indicates that for usa the id is 1, japan is 2, korea is 3.
And on the table it says that for pathid no. 3 ( which is the id for korea) the value is 500 and so on with the others.
I want it to look like this. So then the path will be joined on the second table on its corresponding value. How can I do this on mysql? Thank You
Desired Result
| id | path | value |
------------------------------
| 1 | country/usa | 10000 |
| 2 | country/jpn | 2000 |
| 3 | country/kor | 500 |
You can join on the columns irrespective of the column name as long as the data type match.
SELECT id, path, value
FROM firstTable, secondTable
WHERE id = pathid
If you have same column names on both tables then you need to qualify the name using alias. Say the column names for id were same on both tables then whenever you use id you should mention which table you are referring to. other wise it will complain about the ambiguity.
SELECT s.id, path, value
FROM firstTable f, secondTable s
WHERE f.id = s.pathid
Note that I ommited s. on other columns in select, it will work as long as the second table doesn't have columns with same name.