mysqldatabasesql-likeinner-query

Inner query to match 'LIKE' results from another table


I have a table with data as below:

FILTER Table

**id    filter**    
4638    Aabe    
4639    Aaby    
4640    Aadl    
4641    Aaga    
4642    Aake

SURNAMES Table

**surnames**    
Aaberge    
Aabehut    
Aabyuti    
Aabytis    
Aadlit    
Aagastha    
Aaker

I am trying to write an inner query to retrieve a count of surnames that match filters from the filter table

Below is what i have based on separate select statements

  1. select * from filter
  2. select count(*) from surnames where last_name like 'Aabe%' or last_name like 'Aaby%' group by last_name;

How can I write an inner query for this condition since the record count is large?


Solution

  • select *,
    COUNT(*) from surnames join filters 
    on SURNAMES.last_name LIKE CONCAT(filters.filter,'%') 
    where filters.id > 4215 
    group by last_name