本文介绍了列在选择中无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将如何正确执行此操作:

How I will properly do this:

客户

CusID, CusLname, CusFname, CusMname, CusAddress, CusEmailAdd

订购

OrderID,  Order,   CusID

SQL

Select  Count(OrderID), o.CusID, CusLname, CusFname, CusMname, CusAddress, CusEmailAdd
From Customer c 
Inner join
Order o
On c.CusID = o.CusID
Group By o.CusID

错误列CusLname、CusFname、CusMname、CusAddress、CusEmailAdd"在选择列表中无效,因为它既不包含在聚合函数中也不包含在 GROUP BY 子句中.

为什么我需要在 Group By Clause 中添加这些列?由于我只想按 CusID 对它们进行分组,我将如何选择这些列而不将它们放入分组依据"中?

Why do I need to add those columns in the Group By Clause? What will I do to select those columns without having them in the Group By because I only want to group them by CusID?

推荐答案

有点不同的版本(来自 Gordon Linoff 的建议):

A little bit different version (from that Gordon Linoff suggests):

select
     cnt, c.CusID, CusLname, CusFname, CusMname, CusAddress, CusEmailAdd
from Customer c 
join (
    select  
        count(OrderID) cnt, CusID
    from Order o 
    group by
        CusID
) o on
    c.CusID = o.CusID
order by
    c.CusID // ordering by OrderID and Order is out of logic :)

这篇关于列在选择中无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 14:26