mysql get maser and all details if one detail matches condition -
mysql get maser and all details if one detail matches condition -
i have 2 tables product , specs want product , group_concat(all specs) if @ to the lowest degree 1 spec matches where. have far returns 1 spec matches where.
select p.id, p.name, p.manufacturer group_concat(s.specvalue order s.pid,',') product p bring together spec s on p.id = s.pid s.specvalue = 'micro' grouping p.id product table | id | name | manufacturer | | 1 | iphone | apple | | 2 | galaxy | samsung | | 3 | note | samsung | ------------------------------ spec table | id | pid | specname | specvlaue | | 1 | 1 | charger | bad | | 2 | 2 | charger | micro | | 3 | 2 | keypad | touch | | 4 | 4 | charger | micro | -----------------------------------
you can utilize next uses in in where clause:
select p.id, p.name, p.manufacturer, group_concat(s.specvalue order s.pid,',') allspecs product p bring together spec s on p.id = s.pid p.id in (select pid spec s1 s1.specvalue = 'micro') grouping p.id see sql fiddle demo.
or can utilize exists:
select p.id, p.name, p.manufacturer, group_concat(s.specvalue order s.pid,',') allspecs product p bring together spec s on p.id = s.pid exists (select pid spec s1 s1.specvalue = 'micro' , p.id = s1.pid) grouping p.id see sql fiddle demo
both give result:
| id | name | manufacturer | allspecs | -------------------------------------------- | 2 | galaxy | samsung | touch,micro | if don't want utilize subquery, utilize having clause filter records value:
select p.id, p.name, p.manufacturer, group_concat(s.specvalue order s.pid,',') allspecs product p bring together spec s on p.id = s.pid grouping p.id having group_concat(s.specvalue order s.pid,',') '%micro%' see sql fiddle demo
mysql
Comments
Post a Comment