How can i combine an IN and a LIKE statement in mysql query?

MySQL 5.5.54

I have two tables, both with the same structure.  I want to select records from table 1 based on the values in table 2.
I have a field, thevalue, which contains values such as:

I have the query:
SELECT * FROM table1 where thevalue  in(SELECT distinct concat('Like ''',left(rangeid,8),'%''') FROM table2)

when I run this query I don't get an error but 0 rows are returned.  when I run the inner select statement separately it returns

'Like \'181-1-00%\''
'Like \'182-1-00%\''
'Like \'183-2-00%\''
'Like \'187-2-00%\''
'Like \'188-2-00%\''

Can anyone suggest how I can return the rows in table1 where the first 8 characters of the thevalue field match those in the thevalue field of table2?
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.

Pawan KumarDatabase ExpertCommented:
Please use like this ..


SELECT * FROM table1 a 
INNER JOIN table2 b ON LEFT(b.rangeid,8) LIKE CONCAT('%' , a.thevalue , '%' )

Open in new window


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, and consider normalization.. this kind of query will be a performance nightmare on large sets.
Julian HansenCommented:
It appears from your query you are trying to create a dynamic LIKE statement - you have shown us values from one table - what do the corresponding values from the other table look like - the same? If so why not do this

SELECT * FROM table1 WHERE thevalue in (SELECT DISTINCT rangeid FROM table2)

Open in new window

If not show us the other data so we can fix the query.
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

Pawan KumarDatabase ExpertCommented:
If thats what you need then do not use DISTINCT and IN clause. you can directly use this ..
DISTINCT is not required at all as we are just checking for existence of rows.

SELECT * FROM table1 a
WHERE EXISTS ( SELECT NULL FROM table2 b WHERE a.thevalue  = b.rangeid )

@Author - You just need to handle the where clause if you need any changes.
NemetonaAuthor Commented:
Thank you all.  While Pawan Kumar's solution did not quite work, as I needed to be able to edit the results, it gave me the idea on how to change my query to get what I needed.

SELECT * FROM table1 where left(thevalue,8) in(select left(thevalue,8) from table2)

Many thanks to you all.
NemetonaAuthor Commented:
Thank you
Pawan KumarDatabase ExpertCommented:
welcome glad to help as always :)
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
Query Syntax

From novice to tech pro — start learning today.