不适合 this question 的解决方案但现在有任何动态(重要和必要的)枢轴或其他方式的解决方案,这个返回表?
执行底层查询是选择并返回表
查询:
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
最佳答案
使用 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
关于mysql - 如何动态转换或以相同的方式进行此 mysql 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47853033/