Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Criando combinações de tabela/colunas usando SQL Query ou Laravel SQL Query Builder


Isso deve servir. Não funcionará no mysql 5.7 porque os CTEs recursivos não foram incluídos até mais tarde, mas isso permitirá que você tenha um número variável de atributos e attribute_values ​​por group_id. O violino está aqui .
with recursive allAtts as (
  /* Get our attribute list, and format it if we want; concat(a.title, ':', v.title) looks quite nice */
  SELECT 
    att.group_id,
    att.id,
    CONCAT(v.title) as attDesc,
    dense_rank() over (partition by att.group_id order by att.id) as attRank
FROM table_attributes att 
INNER JOIN table_attribute_values v 
    ON v.group_id = att.group_id 
    AND v.attribute_id = att.id 
 ),
 cte as (
 /* Recursively build our attribute list, assuming ranks are sequential and we properly linked our group_ids */
    select group_id, id, attDesc, attRank from allAtts WHERE attRank = 1
   
         union all 
   
    select 
        allAtts.group_id, 
        allAtts.id, 
        concat_ws('-', cte.attDesc, allAtts.attDesc) as attDesc,
        allAtts.attRank
   from cte 
   join allAtts ON allAtts.attRank = cte.attRank +1
      AND cte.group_id = allAtts.group_id
)
   
/* Our actual select statement, which RIGHT JOINs against the table_groups 
   so we don't lose entries w/o attributes */   
select 
    grp.id,
    concat_ws('-', d.day, qty.quantity, cte.attDesc) as combinations
from cte 
inner join (select group_id, max(attRank) as attID
            from cte
            group by group_id) m on cte.group_id = m.group_id and m.attID = cte.attrank
RIGHT JOIN table_groups grp ON grp.id = cte.group_id 
LEFT JOIN table_days d on grp.id = d.group_id
LEFT JOIN table_quantities qty on grp.id = qty.group_id;