Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use the LIKE operator on a list of strings to compare?

Tags:

sql

mysql

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.

like image 202
Patrick Avatar asked Jan 19 '12 15:01

Patrick


People also ask

How do I compare a string to a list of strings in SQL?

SELECT STRCMP(argument1, argument2); Here, argument1 and argument2 are string type data values which we want to compare. The syntax for using LIKE wildcard for comparing strings in SQL is as follows : SELECT column_name1, column_name2,...

What is like %% in SQL?

The LIKE operator is used in a WHERE clause to search for a specified pattern in a column. There are two wildcards often used in conjunction with the LIKE operator: The percent sign (%) represents zero, one, or multiple characters. The underscore sign (_) represents one, single character.

Can we use in operator with LIKE operator?

the LIKE operation is not permitted to be used with IN.

How use like operator for multiple values in SQL?

The SQL LIKE clause is used to compare a value to similar values using wildcard operators. There are two wildcards used in conjunction with the LIKE operator. The percent sign represents zero, one or multiple characters. The underscore represents a single number or character.


2 Answers

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.

like image 167
anubhava Avatar answered Oct 07 '22 21:10

anubhava


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 |
+-------+
like image 40
Devart Avatar answered Oct 07 '22 21:10

Devart