简体   繁体   English

列 'seller.seller_Name' 在 select 列表中无效,因为它不包含在聚合 function 或 GROUP BY 子句中

[英]Column 'seller.seller_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause

I was creating an SQL query but ran into some problems with aggregate function or the group by clause.我正在创建一个 SQL 查询,但遇到了聚合 function 或 group by 子句的一些问题。 I've never use stack overflow before so please forgive my poor formatting and grammar.我以前从未使用过堆栈溢出,所以请原谅我糟糕的格式和语法。 These are the codes below.这些是下面的代码。

**/ Create Table Seller / **/创建表卖家/

create table seller60 (
    seller_ID int identity (1000,1) not null,
    seller_Name varchar(100) not null,
    seller_userName varchar (100) unique,
    seller_password varchar (100) not null,
    item_ID int not null,
    contactID int not null,
    creditCardID int not null,
    primary key(seller_ID), foreign key(contactID) references contactInfo(contact_ID),foreign key(creditCardID) references creditCard60(creditCard_ID)
);

**/ Create Table Item / **/创建表项/

create table item60(
    itemID int identity(1000,1) not null,
    itemName varchar(100) not null,
    itemDesc varchar (100),
    item_initialPrice money,
    item_Quantity int,
    ownerID int not null,
    condition varchar(100) not null,
    primary key( itemID), foreign key(ownerID) references seller60(seller_ID)
);

**/ The most active seller / **/最活跃的卖家/

SELECT a.ownerID, b.seller_Name 
FROM item AS a 
INNER JOIN seller AS b ON a.ownerID = b.seller_ID 
GROUP BY a.ownerID 
ORDER BY COUNT(a.itemID) DESC set rowcount 1;

The problem here is that the select clause is not consistent with the group by clause: you have non-aggrgated column seller60(seller_name) in the select clause, but not in the group by clause, and MySQL rightfully complains about that.这里的问题是select子句与group by子句不一致:您在select子句中有非聚合列seller60(seller_name) ,但不在group by子句中,并且 Z62A4104B9594673AZ

One method is to add that column to the group by clause.一种方法是将该列添加到group by子句。 A somehow smarter alternative is group by column seller60(seller_id) : this is identical to item(owner_id) (that is a foreign key to that column);一个更聪明的选择是按列seller60(seller_id) :这与item(owner_id)相同(即该列的外键); and, since that is the primary key of seller60 , you can then add as many columns from that table as you want in the select clause without breaking the query (that's the concept of functionaly dependent column, which MySQL understands).并且,由于这是seller60的主键,因此您可以在select子句中添加任意数量的列,而不会破坏查询(这是功能相关列的概念,MySQL 理解)。

select s.* 
from item60 i
inner join seller60 s on i.ownerid = s.seller_id 
group by s.seller_id 
order by count(*) desc limit 1;

Note that this uses meaningful table aliases, rather than random letters such as a or b .请注意,这使用有意义的表别名,而不是随机字母,例如ab I also fixed the limit clause, whose syntax was off.我还修复了语法关闭的limit子句。

Finally: you could also get the same result with a correlated subquery - and then you don't need to worry about aggregation at all.最后:您也可以使用相关子查询获得相同的结果 - 然后您根本不需要担心聚合。

select s.*
from seller60 s
order by (select count(*) from item60 i where i.ownerid = s.seller_id) desc limit 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 列 'City.Name' 在 select 列表中无效,因为它不包含在聚合 function 或 GROUP BY 子句中 - Column 'City.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 在选择列表中,列“ tbl_Stock.Item_Name”无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中 - Column 'tbl_Stock.Item_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 列“Users.Name”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中 - Column 'Users.Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 列“faculty.f_name”在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中 - Column 'faculty.f_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 选择列表中的“ Product.product_name”列无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中 - Column 'Product.product_name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 柱 <name> 在选择列表中无效,因为它未包含在聚合函数或GROUP BY子句中 - Column <name> is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 选择列表中的“ Course.Course_Name”列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中 - Column 'Course.Course_Name' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause 在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中 - invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause GROUP BY给出“列在选择列表中无效,因为该列未包含在聚合函数或GROUP BY子句中” - GROUP BY gives “Column is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause” iif 函数中的列在选择列表中无效,因为它不包含在聚合函数或 GROUP BY 子句中 - Column in iif function invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM