How can I use the LIKE operator on a list of strings to compare?
Solution 1
You could do something like this -
SELECT FIND_IN_SET(
'bigD',
REPLACE(REPLACE('barfy,max,whiskers,champ,big-D,Big D,Sally', '-', ''), ' ', '')
) has_petname;
+-------------+
| has_petname |
+-------------+
| 5 |
+-------------+
It will give a non-zero value (>0) if there is a pet_name we are looking for.
But I'd suggest you to create a table petnames
and use SOUNDS LIKE function to compare names, in this case 'bigD' will be equal to 'big-D', e.g.:
SELECT 'bigD' SOUNDS LIKE 'big-D';
+---------------------------+
| 'bigD'SOUNDS LIKE 'big-D' |
+---------------------------+
| 1 |
+---------------------------+
Example:
CREATE TABLE petnames(name VARCHAR(40));
INSERT INTO petnames VALUES
('barfy'),('max'),('whiskers'),('champ'),('big-D'),('Big D'),('Sally');
SELECT name FROM petnames WHERE 'bigD' SOUNDS LIKE name;
+-------+
| name |
+-------+
| big-D |
| Big D |
+-------+
Solution 2
For this task I would suggest making use of RegExp capabilities in MySQL like this:
select * from EMP where name RLIKE 'jo|ith|der';
This is case insensitive match and will save from multiple like / OR conditions.
Solution 3
As first step put all static values in any temporary table, this would be lookup dictionary.
SELECT * FROM Table t
WHERE EXISTS (
SELECT *
FROM LookupTable l
WHERE t.PetName LIKE '%' + l.Value + '%'
)
Related videos on Youtube
Patrick
I am a lead developer with two degrees in computer science. I have a background in computer (desktop and laptop) and projector repair as well as tech support for multiple companies. Most of my development is done in PHP and JavaScript. I have dabbled in Ruby, Java, Python, and others. In my position I find myself mostly working in Drupal 8 and Laravel. But I have worked with WordPress, OpenCart, Magento (ick!), OSCommerce, CodeIgniter, CakePHP, Laravel, and I have a dabbled in numerous other frameworks and CMSes. I am not shy at trying new libraries, tools, and systems. I have worked with MySQL and other RDBMS solutions. I have spent a little bit of time learning MongoDB and CouchDB. My skills are wide and varying. I am a hiring manager and have lead multiple teams of developers for several projects. My skills continue to grow and enhance. I am a nerd through and through and try to absorb and analyze every bit of knowledge I come across. My interests span through everything. Astronomy, Physics, Geology, Woodworking, Home Repair, Building and Creating things, and anything electronics.
Updated on June 04, 2022Comments
-
Patrick almost 2 years
I have a query I need to run on almost 2000 strings where it would be very helpful to be able to do a list like you can with the "IN" operator but using the LIKE comparison operation.
For example I want to check to see if pet_name is like any of these (but not exact): barfy, max, whiskers, champ, big-D, Big D, Sally
Using like it wouldn't be case sensitive and it can also have an underscore instead of a dash. Or a space. It will be a huge pain in the ass to write a large series of OR operators. I am running this on MySQL 5.1.
In my particular case I am looking for file names where the differences are usually a dash or an underscore where the opposite would be.
-
sll over 12 yearsRigth, missed that (added now). I believe the main challenge for OP was processing multiple entries, I believe he know how to apply
LIKE
but anyway you was right -
Patrick over 12 yearsI am not sure I follow on two accounts, first how to get it into the temp table (which I can look up) and second how this operation is working. Specifically I looks like in the subquery you are doing a comparison of a value from the calling query directly within that subquery. Is that even possible?
-
JSuar over 12 yearsWould the OP need to enter static values with a wildcard (eg big_d) to account for different values (big-D, Big D, etc) like the OP described? Also, LCASE() needed as well?
-
JSuar over 12 years@pthurmond, subquery is possible: dev.mysql.com/doc/refman/5.0/en/…
-
Patrick over 12 yearsWill this ignore the difference between a dash and an underscore?
-
Patrick over 12 years+1 for the interesting concept. But I don't think you follow what I am trying to accomplish (that or I am just not following your implementation). Basically I have files that have sister files they need to be associated with. But when the client generated a lot of these sister files (multiple people did this) they had some format changes automatically or manually happen. So "bobs-green-80-hat.eps" sometimes became "bobs-green_80-hat.eps.jpg" and other times it became "BOBS-GREEN-80-HAT.EPS.JPG", depending on who did it.
-
Devart over 12 yearsDo you store file names in the table?
-
anubhava over 12 yearsSure you can run this query to get that behavior:
select * from EMP where replace(name, '-', '_') RLIKE 'jo|ith|der';
-
Patrick over 12 yearsWell that is the tricky part. We had 2 terabytes of files that consisted of an eps, a psd, or a tif file that had a paired jpg file generated from it for presentation on a web page. Every file was created by the client and they had to send this hard drive with all the files on it around to different locations so that they could add their files. This created a lot of inconsistencies. To make matters worse, once we got the drive we needed to get the data pages up on the site for them quickly so we had to upload the jpgs to the server and then send the drive to our host to get the rest up.
-
Patrick over 12 yearsThat way we could get the basic data pages setup and they could add tagging data while we came back later (once our host got the drive and hooked it up to the server for the direct transfer) and attach the eps or other formatted file to the uploaded pages based on the name. The problem is these inconsistencies meant that the jpgs and paired file were not always identically named. So I had to write a script that played a guessing game as to what was the correct file to attach. All in all this involved over 17,000 pairs of files (over 32,000 files in total).
-
Patrick over 12 yearsIn the end there were about 1,700 files that I could not get identified. So I was trying to find out which nodes didn't have a matching pair.
-
Patrick over 12 yearsLike I said, this whole thing was a huge pain in the ass.
-
Stephen Saucier about 8 yearsThis supremely useful. Copy a list of results from another query, change each line break to a pipe, and this works incredibly well.
-
anubhava over 7 years
select replace('abc\nline', '\n', ':');
replaced newline with: