0

In Mysql I have a table like this

enter image description here

And I want to filter only those records that can either start with or ends with all the below possible combinations in the groupname column. (sm.code)

196
328
2600
2708
3666
4453
4460
4468
4469

I tried a query but have some problem in finding all possible combinations.

SQL:-

select cr.groupname,cr.description,cr.higher_limit,cr.lower_limit, ce.severity from compatibility_rule as cr 
        join starting_material as sm on sm.id=cr.starting_material_id 
        join component as c on c.id=sm.component_id and c.cas_number in ('67-56-1','67-64-1')
        join compatibility_error as ce on ce.id=cr.compatibility_error_id 
        and (cr.groupname like concat('%: ', sm.code) and cr.groupname like concat(sm.code, ' : %'))  
        and c.active=true and sm.active=true and cr.active=true 
        order by cr.groupname;  

I might have to tweek something in this part, but not sure how.

and (cr.groupname like concat('%: ', sm.code) and cr.groupname like concat(sm.code, ' : %'))  

Update:- Also posted simplified question in here

I have now created a sample on SQLFiddle below link

SQL Fiddel: http://sqlfiddle.com/#!9/54714e/39

I would like to filter only the group names that contains various combinations of values in the sm table. i.e. 100,101,2000.

The below query returns other combinations too like

100:2000
100:2001
101:2000
101:2001

SQL:-

select distinct d.groupname from docs d, sm s  where d.groupname like concat(s.code,'%') or d.groupname like concat('%',s.code);
Jay
  • 9,189
  • 12
  • 56
  • 96

1 Answers1

0

based on the schema that appears in http://sqlfiddle.com/#!9/54714e/1 you can use this query

select distinct * from (select d.id , d.groupname from docs as d,sm as s where d.groupname like (CONCAT("%",s.code)) or (CONCAT(s.code,"%"))) as t;