如何对这个mysql查询进行动态调整或相同的方式?

问题描述 投票:1回答:1

不适合this question的解决方案,但现在任何动态(重要和必要)枢轴或其他方式的解决方案,这个返回表?

执行底部查询是select和return table

查询:

SELECT p.product_id,pf.filter_id
FROM  oc_product p 
LEFT JOIN oc_product_filter pf 
on p.product_id=pf.product_id
where p.product_id in(96621,97026) and pf.filter_id in (1901,1855 )
group by p.product_id,pf.filter_id

处理结果:

product_id  filter_id
96621           1855
96621           1901
97026           1901
97026           1610

我想结果这个:

product_id  filter_id
96621           1855
96621           1901

但结果如下:

  product_id        filter_id
    96621           1855,1901  
    97026           1901,1610
mysql
1个回答
1
投票

使用GROUP_CONCAT来旋转表格:

SELECT p.product_id,GROUP_CONCAT(pf.filter_id)
FROM  oc_product p 
LEFT JOIN oc_product_filter pf 
on p.product_id=pf.product_id
where p.product_id in(96621,97026) and pf.filter_id in (1901,1855 )
group by p.product_id

这将返回:

  product_id        filter_id
    96621           1855,1901  
    97026           1901,1610
© www.soinside.com 2019 - 2024. All rights reserved.