mysql - 如何动态转换或以相同的方式进行此 mysql 查询?

标签 mysql

不适合 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/

相关文章:

php - 如何使用 LIKE 忽略 mysql 搜索查询中的空格?

mysql - 如果不存在则插入,如果存在且条件为真则更新

android - 除了 Listview 之外,还有其他替代方法可以在 Android 中显示 MySQL 服务器数据吗?

MySQL性能;大数据表还是多数据表?

需要 MySQL 语法帮助

mysql - 提取不同的成员名称及其计数

php - 在注册表中选择下拉选项

sql - 对每行的列进行 SUM 计算的 Select 查询应该是什么?

mysql - Order By 两个表 SQL 中的列

java - 如何在不使用IDE/外部工具的情况下建立Java程序和数据库之间的连接?