zoukankan      html  css  js  c++  java
  • 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句

    在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句

     突然看到这个问题,脑袋一蒙,不知道啥意思,后来想想,试图把select里的选项放到后面,问题自然解决!
     
    下面这个就是报“orderdate
    select shipcountry,sum(shipvia) as totalvia,OrderDate as thefirsttime from orders group by shipcountry,相应的从网上看到其他的朋友也有这样的问题

    比如要显示authors表中的au_fname,au_lname,zip, city,state信息,并且按city分组(相同city的排列在一起)

    错误的表达:
    select au_fname,au_lname,zip, city,state
    from authors
    group by city

    就会出现:服务器: 消息 8120,级别 16,状态 1,行 1
    列 'authors.au_fname' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 'authors.au_lname' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    ...... 的错误提示。

    正确的表达:
    select au_fname,au_lname,zip, city,state
    from authors
    group by city,au_lname,au_fname,zip,state

    即指定 GROUP BY 时,选择列表中任一非聚合表达式内的所有列都应包含在 GROUP BY 列表中,或者 GROUP BY 表达式必须与选择列表表达式完全匹配。

    其实还有更好的办法:
    select au_fname,au_lname,zip, city,state
    from authors
    order by city
    即使用order by 子句

    还是authors表,要求在每个城市取一个作者,显示该作者的左右信息:
    可以用如下表达:
    select * from authors
    where au_id in
    (select min(au_id)
    from authors
    group by city)
    其实是分组后取最小ID的

    比如要显示authors表中的au_fname,au_lname,zip, city,state信息,并且按city分组(相同city的排列在一起)

    错误的表达:
    select au_fname,au_lname,zip, city,state
    from authors
    group by city

    就会出现:服务器: 消息 8120,级别 16,状态 1,行 1
    列 'authors.au_fname' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    服务器: 消息 8120,级别 16,状态 1,行 1
    列 'authors.au_lname' 在选择列表中无效,因为该列既不包含在聚合函数中,也不包含在 GROUP BY 子句中。
    ...... 的错误提示。

    正确的表达:
    select au_fname,au_lname,zip, city,state
    from authors
    group by city,au_lname,au_fname,zip,state

    即指定 GROUP BY 时,选择列表中任一非聚合表达式内的所有列都应包含在 GROUP BY 列表中,或者 GROUP BY 表达式必须与选择列表表达式完全匹配。

    其实还有更好的办法:
    select au_fname,au_lname,zip, city,state
    from authors
    order by city
    即使用order by 子句

    还是authors表,要求在每个城市取一个作者,显示该作者的左右信息:
    可以用如下表达:
    select * from authors
    where au_id in
    (select min(au_id)
    from authors
    group by city)
    其实是分组后取最小ID的行

  • 相关阅读:
    运算
    使用mysql出现错误:com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column '??????' in 'field list'
    POI-HSSF and POI-XSSF
    java pio项目使用
    mysql安装及卸载
    pdf转word
    【android studio】android studio使用过程中,搜集的一些问题
    github提交失败并报错java.io.IOException: Authentication failed:
    收集的github的东西
    Material Design
  • 原文地址:https://www.cnblogs.com/aimqqroad-13/p/4937517.html
Copyright © 2011-2022 走看看