[英]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
.请注意,这使用有意义的表别名,而不是随机字母,例如
a
或b
。 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.