SQL组通过返回第一行 [英] SQL group by returns just first row

查看:146
本文介绍了SQL组通过返回第一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个'id'和'layout plan'列的表



我需要查看所有具有相同布局计划的行



我使用这个查询。

  select * 
from project_layout
group by layout_plan
having count(layout_plan)> 1

但是这个查询只返回第一行。



我想查看所有使用相同布局计划的组。

除MySQL以外的数据库如果使用不包含聚合的列,则会发生错误。但是,MySQL将从组的行中返回一个不确定的值。



检索 layout_plan 组中的所有行不止一行,您可以使用:

  select * 
from project_layout
其中layout_plan位于

)从project_layout中选择layout_plan

$ by
layout_plan

count(*)> 1


I have a table which contains two columns 'id' and 'layout plan'

I need to see all those rows which have the same layout plan

I use this query.

select * 
from project_layout 
group by layout_plan 
having count(layout_plan) > 1

But this query is only returning the first row.

I want to see all the groups with the same layout plan.

解决方案

Databases other than MySQL would give an error if you use a column that's not grouped without an aggregate. But MySQL will return an indeterminate value from among the group's rows.

To retrieve all the rows in layout_plan groups with more than one row, you could use:

select  *
from    project_layout
where   layout_plan in
        (
        select  layout_plan 
        from    project_layout 
        group by 
               layout_plan 
        having 
               count(*) > 1
        )

这篇关于SQL组通过返回第一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆