Is it possible to GROUP BY
more than one column in a MySQL SELECT
query? For example:
在MySQL SELECT查询中,是否可以对多个列进行分组?例如:
GROUP BY fV.tier_id AND 'f.form_template_id'
7 个解决方案
#1
186
GROUP BY col1, col2, col3
#2
54
Yes, you can group by multiple columns. For example,
是的,您可以按多列进行分组。例如,
SELECT * FROM table
GROUP BY col1, col2
The results will first be grouped by col1, then by col2. In MySQL, column preference goes from left to right.
结果首先由col1分组,然后由col2分组。在MySQL中,列首选项从左到右。
#3
13
group by fV.tier_id, f.form_template_id
#4
13
Yes, but what does grouping by more two columns mean? Well, it's the same as grouping by each unique pair per row. The order you list the columns changes the way the rows are sorted.
是的,但是多两列分组意味着什么?它和每一行中每对唯一对的分组是一样的。列出列的顺序改变了对行排序的方式。
In your example, you would write
在您的示例中,您将编写
GROUP BY fV.tier_id, f.form_template_id
GROUP BY阵线。tier_id,f.form_template_id
Meanwhile, the code
与此同时,代码
GROUP BY f.form_template_id, fV.tier_id
集团由f。form_template_id,fV.tier_id
would give similar results, but sorted differently.
会得到相似的结果,但排序方式不同。
#5
3
To use a simple example, I had a counter that needed to summarise unique IP addresses per visited page on a site. Which is basically grouping by pagename and then by IP. I solved it with a combination of DISTINCT and GROUP BY.
为了使用一个简单的示例,我有一个计数器,它需要在一个站点上每访问的页面总结唯一的IP地址。基本上是通过pagename然后IP进行分组。我用不同的和群体的组合来解它。
SELECT pagename, COUNT(DISTINCT ipaddress) AS visit_count FROM log_visitors GROUP BY pagename ORDER BY visit_count DESC;
#6
0
GROUP BY CONCAT(col1, '_', col2)
#7
0
If you prefer (I need to apply this) group by two columns at same time, I just saw this point:
如果你喜欢(我需要把这个)组同时分成两列,我就看到了这一点:
SELECT CONCAT (col1, '_', col2) AS Group1 ... GROUP BY Group1
#1
186
GROUP BY col1, col2, col3
#2
54
Yes, you can group by multiple columns. For example,
是的,您可以按多列进行分组。例如,
SELECT * FROM table
GROUP BY col1, col2
The results will first be grouped by col1, then by col2. In MySQL, column preference goes from left to right.
结果首先由col1分组,然后由col2分组。在MySQL中,列首选项从左到右。
#3
13
group by fV.tier_id, f.form_template_id
#4
13
Yes, but what does grouping by more two columns mean? Well, it's the same as grouping by each unique pair per row. The order you list the columns changes the way the rows are sorted.
是的,但是多两列分组意味着什么?它和每一行中每对唯一对的分组是一样的。列出列的顺序改变了对行排序的方式。
In your example, you would write
在您的示例中,您将编写
GROUP BY fV.tier_id, f.form_template_id
GROUP BY阵线。tier_id,f.form_template_id
Meanwhile, the code
与此同时,代码
GROUP BY f.form_template_id, fV.tier_id
集团由f。form_template_id,fV.tier_id
would give similar results, but sorted differently.
会得到相似的结果,但排序方式不同。
#5
3
To use a simple example, I had a counter that needed to summarise unique IP addresses per visited page on a site. Which is basically grouping by pagename and then by IP. I solved it with a combination of DISTINCT and GROUP BY.
为了使用一个简单的示例,我有一个计数器,它需要在一个站点上每访问的页面总结唯一的IP地址。基本上是通过pagename然后IP进行分组。我用不同的和群体的组合来解它。
SELECT pagename, COUNT(DISTINCT ipaddress) AS visit_count FROM log_visitors GROUP BY pagename ORDER BY visit_count DESC;
#6
0
GROUP BY CONCAT(col1, '_', col2)
#7
0
If you prefer (I need to apply this) group by two columns at same time, I just saw this point:
如果你喜欢(我需要把这个)组同时分成两列,我就看到了这一点:
SELECT CONCAT (col1, '_', col2) AS Group1 ... GROUP BY Group1