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


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
Who is Participating?

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

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.

This question have already answer. Look this article:

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
        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' );

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

Open in new window

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
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

From novice to tech pro — start learning today.