I'd like to use an SQL query to find and replace multiple values. I've had a look at this question that shows the following answer:
UPDATE
YourTable
SET
Column1 = REPLACE(Column1,'a','b')
WHERE
Column1 LIKE '%a%'
How can I find and replace multiple values instead of just the one?
My data is like the following, there's hundreds of rows, I'm specifically wanting to target each product_id:123:
subscription_id,products
"128","product_id:268|quantity:1|total:3.15|meta:|tax:0;product_id:267|quantity:1|total:2.97|meta:|tax:0"
I need to replace the product id's with new products id's. So it'll be "everything matching 268 will become 195" and "everything matching 267 will become 194".
Is there an efficient way to do it other than taking the code block above and using that for each product. Can I be done with one sweep through?
Simplest possible way would be to chain REPLACEs together, but considering the concatenated nature of the field you need to be sure you don't inadvertently target something that's not actually a product_id value. You can mitigate this by including some contextual content from the string value itself:
UPDATE YourTable
SET products = REPLACE(REPLACE(products, "product_id:267|", "product_id:194|"), "product_id:268|", "product_id:195|");
DBFiddle | MySQL 5.6 Reference Manual :: 13.2.8 REPLACE Statement
If there's some variability in how these strings might appear in a given field and you're running MySQL >=8.0, you can leverage something like REGEXP_REPLACE() to perform this same replacement using a defined RegExp pattern.
Yes, there are ways. For example, you can create a table like
replacements(id, oldval, newval)
and do the following:
UPDATE
Yourtable
JOIN
replacements
ON
Yourtable.Column1 LIKE CONCAT('%', replacements.oldval, '%')
SET
Yourtable.Column1 = REPLACE(Yourtable.Column1, replacements.oldval, replacements.newval);
The problem is that you would need to fill replacements with the pairs of oldval-newval, but MySQL cannot guess that. Insertion is as simple (assuming that id is auto_increment) as
INSERT INTO replacements(oldval, newval) VALUES
('a', 'b'),
('c', 'd'),
...
;
Related
I have a table say, ITEM, in MySQL that stores data as follows:
ID FEATURES
--------------------
1 AB,CD,EF,XY
2 PQ,AC,A3,B3
3 AB,CDE
4 AB1,BC3
--------------------
As an input, I will get a CSV string, something like "AB,PQ". I want to get the records that contain AB or PQ. I realized that we've to write a MySQL function to achieve this. So, if we have this magical function MATCH_ANY defined in MySQL that does this, I would then simply execute an SQL as follows:
select * from ITEM where MATCH_ANY(FEAURES, "AB,PQ") = 0
The above query would return the records 1, 2 and 3.
But I'm running into all sorts of problems while implementing this function as I realized that MySQL doesn't support arrays and there's no simple way to split strings based on a delimiter.
Remodeling the table is the last option for me as it involves lot of issues.
I might also want to execute queries containing multiple MATCH_ANY functions such as:
select * from ITEM where MATCH_ANY(FEATURES, "AB,PQ") = 0 and MATCH_ANY(FEATURES, "CDE")
In the above case, we would get an intersection of records (1, 2, 3) and (3) which would be just 3.
Any help is deeply appreciated.
Thanks
First of all, the database should of course not contain comma separated values, but you are hopefully aware of this already. If the table was normalised, you could easily get the items using a query like:
select distinct i.Itemid
from Item i
inner join ItemFeature f on f.ItemId = i.ItemId
where f.Feature in ('AB', 'PQ')
You can match the strings in the comma separated values, but it's not very efficient:
select Id
from Item
where
instr(concat(',', Features, ','), ',AB,') <> 0 or
instr(concat(',', Features, ','), ',PQ,') <> 0
For all you REGEXP lovers out there, I thought I would add this as a solution:
SELECT * FROM ITEM WHERE FEATURES REGEXP '[[:<:]]AB|PQ[[:>:]]';
and for case sensitivity:
SELECT * FROM ITEM WHERE FEATURES REGEXP BINARY '[[:<:]]AB|PQ[[:>:]]';
For the second query:
SELECT * FROM ITEM WHERE FEATURES REGEXP '[[:<:]]AB|PQ[[:>:]]' AND FEATURES REGEXP '[[:<:]]CDE[[:>:]];
Cheers!
select *
from ITEM where
where CONCAT(',',FEAURES,',') LIKE '%,AB,%'
or CONCAT(',',FEAURES,',') LIKE '%,PQ,%'
or create a custom function to do your MATCH_ANY
Alternatively, consider using RLIKE()
select *
from ITEM
where ','+FEATURES+',' RLIKE ',AB,|,PQ,';
Just a thought:
Does it have to be done in SQL? This is the kind of thing you might normally expect to write in PHP or Python or whatever language you're using to interface with the database.
This approach means you can build your query string using whatever complex logic you need and then just submit a vanilla SQL query, rather than trying to build a procedure in SQL.
Ben
I was looking for a way to exclude values with a '_' in the results set from a mysql database.
Why would the following sql statement return no results?
select questionKey
from labels
where set_id = 674
and questionKey like 'Class%'
and questionKey not like '%_%' ;
which was the first sql I tried where as
select questionKey
from labels
where set_id = 674
and questionKey like 'Class%'
and locate('_',questionKey) = 0 ;
returns
questionKey
ClassA
ClassB
ClassC
ClassD
ClassE
ClassF
ClassG
ClassNPS
ClassDis
which is the result I wanted. Both SQL statements appear to me to be logically equivalent though they are not.
As tadman and PM77 already pointed out, it's a special character. If you want to use the first query, try to escape it like this (note the backslash):
select questionKey
from labels
where set_id = 674
and questionKey like 'Class%'
and questionKey not like '%\_%' ;
In the LIKE context _ takes on special meaning and represents any single character. It's the only one other than % that means something here.
Your LOCATE() version is probably the best here, though it's worth noting that doing table scans like this can get cripplingly slow on large amounts of data. If underscore represents something important you might want to have a flag field you can set and index.
You could also use a regular expression to try and match records with a single condition:
REGEXP '^Class[^_]+'
one of my table contains column path stores the URL example:\xyz\attachments, \abc\attachments, etc total i have 16 combinations to replace
i found rows by using rlike in where clause 'abc|xyz|'
have to update xyz with xxx or abc with yyyy
i am not sure how to update these part of the values of column. Is it possible using single query or i have to write 16 queries to do that? please advise here
This is not reliable, but is doable. Basically nested replace() calls:
UPDATE ...
SET yourfield = REPLACE(REPLACE(yourfield, '\\xyz', 'newtext'), '\\abc', 'othertext')
Note that if xyz or abc can appear in multiple places in either string, you may end up replacing something that shouldn't have been.
I am trying to write a Query to find if a string contains part of the value in Column (Not to confuse with the query to find if a column contains part of a string).
Say for example I have a column in a table with values
ABC,XYZ
If I give search string
ABCDEFG
then I want the row with ABC to be displayed.
If my search string is XYZDSDS then the row with value XYZ should be displayed
The answer would be "use LIKE".
See the documentation: https://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html
You can do WHERE 'string' LIKE CONCAT(column , '%')
Thus the query becomes:
select * from t1 where 'ABCDEFG' LIKE CONCAT(column1,'%');
If you need to match anywhere in the string:
select * from t1 where 'ABCDEFG' LIKE CONCAT('%',column1,'%');
Here you can see it working in a fiddle:
http://sqlfiddle.com/#!9/d1596/4
Select * from table where #param like '%' + col + '%'
First, you appear to be storing lists of things in a column. This is the wrong approach to storing values in the database. You should have a junction table, with one row per entity and value -- that is, a separate row for ABC and XYZ in your example. SQL has a great data structure for storing lists. It is called a "table", not a "string".
If you are stuck with such a format and using MySQL, there is a function that can help:
where find_in_set('ABC', col)
MySQL treats a comma delimited string as a "set" and offers this function. However, this function cannot use indexes, so it is not particularly efficient. Did I mention that you should use a junction table instead?
I'm trying to write the following statement:
WHERE field LIKE 'Pandora' AND field Not Like 'radio', 'digital', 'internet';
Translation: Select where field is like Pandora and not like radio, digital, or internet.
Is there a way to write this statement without writing Not Like 3 times with ANDs in between?
Thank you
If "field" is not just single words, you would need to do something like this:
SELECT * FROM table WHERE field LIKE '%Pandora%' AND field NOT LIKE '%radio%' AND field NOT LIKE '%internet%' and field NOT LIKE '%digital%';
First of all, your query is redundant, in that if field is LIKE 'pandora', then the other conditions will by default return false.
There is no possible way that field can be equal to 'Pandora', 'radio', 'digital', and 'internet'.
As a result, you can simplify your query using the following example:
SELECT *
FROM example
WHERE field = 'Pandora';
If the two conditions represent two separate fields, then you can use the REGEXP operator to enforce the DRY principle while still allowing for further pattern matching:
SELECT *
FROM example
WHERE field_1 = 'Pandora'
AND field_2 NOT REGEXP '^(radio|digital|internet)$';
If you're searching for specific words, you can use NOT IN()
WHERE field LIKE 'Pandora' AND field NOT IN('radio', 'digital', 'internet');
If you need the wildcard % in your search you'll need to use multiple LIKEs.