我想执行这个查询:SQL INNER JOIN实体
-- The most expensive item sold ever
SELECT
c.itemID, c.itemName
FROM
item AS c
JOIN
(SELECT
b.itemID as 'itemid', MAX(b.item_initialPrice) AS 'MaxPrice'
FROM
buyeritem AS a
INNER JOIN
item AS b ON a.item_ID = b.itemID) AS d ON c.itemID = d.itemid
GROUP BY
c.itemID, c.itemName;
我item
表看起来像这样:
create table item
(
itemID int IDENTITY(1000, 1) NOT NULL,
itemName varchar(15) NOT NULL,
Item_desc varchar(255),
Item_initialPrice MONEY,
ItemQty int,
ownerID int NOT NULL,
condition varchar(20) NOT NULL,
PRIMARY KEY (itemID),
FOREIGN KEY (ownerID) REFERENCES seller (sellerID)
);
的问题是,列item.itemID
无效在选择列表中,因为它是不包含在聚合函数或GROUP BY
子句中。我试图在末尾添加一个group by子句
group by c.itemID, c.itemName
但是我仍然得到同样的错误?我真的不知道问题来自哪里。
我也有这个疑问
-- The most active seller(the one who has offered the most number of items)
SELECT
a.ownerID, b.sellerName
FROM
item AS a
INNER JOIN
seller AS b ON a.ownerID = b.sellerID
GROUP BY
a.ownerID, b.sellerName
ORDER BY
COUNT(a.itemID) DESC;
我想补充itemQty
与item
表ownerID
和sellerName
上述以来,这将是实现这一最好的方法是什么?
获得第一个直接查询数据库中的工作,然后担心JDBC代码。 –