【问题标题】:MySQL group by given an array of coulmn valuesMySQL按给定的列值数组分组
【发布时间】:2015-10-05 05:48:43
【问题描述】:

我有一个数组 $array=array(B,c) 和一个像这样的表:

id coulmn1
1 A
2 B
3 C
4 C
5 F

我需要将这些分组为我的给定值,如下所示:

 groupby_Column  count
 group_variable   3
 A        1
 F 1

我需要使用 group by 子句来输出上述结果。

【问题讨论】:

  • 你确定这是一个 MySQL 问题吗?看起来您在谈论 HTML 表而不是数据库表。
  • 是的,我已经向你展示了数据库表
  • 请重新格式化表格以显示列表值而不是 html 代码。请在开头澄清array()的含义。
  • 我没有看到输入和输出之间的关系。您需要澄清这一点。
  • 在输入数组中有Bc,但在输出中有AFgroup_variable 是什么?

标签: php mysql sql group-by


【解决方案1】:

仍然不完全清楚你想要什么,但我会试一试:

您在 php 数组中有值。您想对 column1 进行分组,但您想将数组中的值视为一个组。

在上述情况下,您可以创建类似于以下的查询:

select case when column1 in('b','c') then 'group_variable' else column1 end as 'group',
count(id)
from table
group by case when column1 in('b','c') then 'group_variable' else column1 end

您可以使用 implode() 函数从数组中生成 in 子句。

【讨论】:

    猜你喜欢
    • 2014-07-24
    • 2016-10-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多