SQL: Can you combine LIKE and IN within a single query?

Hi

Can you combine LIKE and IN within a single MYSQL query?

I have a list of codes I need to search for something like this

select * FROM mytable WHERE codes LIKE '%ABC%' OR code LIKE '%XYZ%';

Open in new window


The problem is the column codes  contains a coma separated list eg "ABC,EFG,XYZ"  so a standard IN clause wont work
LVL 1
trevor1940Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MishaProgrammerCommented:
This question have already answer. Look this article:
https://stackoverflow.com/questions/1865353/combining-like-and-in-for-sql-server
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ste5anSenior DeveloperCommented:
Well, normalization is your friend. Otherwise you need a string split function. Using the built-in (SQL Server 2016+):

DECLARE @Unnormalized TABLE
    (
        ID INT IDENTITY ,
        Payload NVARCHAR(255) NOT NULL
    );

INSERT INTO @Unnormalized ( Payload )
VALUES ( N'abc,efg,xyz' ) ,
       ( N'abc,xyz' ) ,
       ( N'efg,xyz' ) ,
       ( N'xyz' ) ,
       ( N'efg' ) ,
       ( N'abc' );

SELECT *
FROM   @Unnormalized U
WHERE  EXISTS (   SELECT *
                  FROM   STRING_SPLIT(U.Payload, ',') SS
                  WHERE  SS.value IN ( 'abc', 'xyz' ));

Open in new window

0
trevor1940Author Commented:
I went with first comment because it is simpler to code via external perl script
Also The second comment mentions  Using the built-in (SQL Server 2016+) unsure if MYSQL has this function
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.