mysql数据检索问题

时间:2010-11-27 16:23:30

标签: mysql group-by

租赁表:

CREATE TABLE RENTAL
(
TransactionNo int NOT NULL AUTO_INCREMENT,
MemberID int NOT NULL, 
ItemNo char(3) NOT NULL,
RentalEmployeeID varchar(30),
ReturnEmployeeID varchar(30), 
Checkout_date DATE, 
Checkin_date DATE,
Return_date DATE,
ItemQuantity int(11) NOT NULL,
TotalPrice DOUBLE(10,2) NOT NULL,
ItemFee DOUBLE(10,2),
PRIMARY KEY(TransactionNo),
FOREIGN KEY(MemberID) REFERENCES Member(MemberID),
FOREIGN KEY(ItemNo) REFERENCES Item(Itemno),
FOREIGN KEY(RENTALEMPLOYEEID) REFERENCES Employee(EmployeeID),
FOREIGN KEY(RETURNEMPLOYEEID) REFERENCES Employee(EmployeeID)
)

我正在尝试检索在同一天购买了至少2件商品的所有客户的查询;我仍然无法完成它。是否需要嵌套子句?

我的陈述:

SELECT m.MemberID, r.`checkout_date`, SUM(r.itemquantity)
FROM RENTAL AS r, MEMBER AS m
WHERE r.MemberID = m.MemberID
GROUP BY  m.MemberID, r.`checkout_date`
HAVING SUM (r.itemquantity) > 1

然而,这个新陈述将给我我想要的东西;因为租赁信息不是必需的(NULL),所以它总结了所有的退货项目,而不仅仅是租赁。

2 个答案:

答案 0 :(得分:0)

你不需要嵌套子句 - 'GROUP BY'可以解决问题。

SELECT m.MemberID, r.`checkout_date`, COUNT(r.itemno)
FROM RENTAL AS r, MEMBER AS m
WHERE r.MemberID = m.MemberID
GROUP BY r.MemberID, `r.checkout_date`

答案 1 :(得分:0)

              group by memberid, checkoutdate having count(transactionid) > 1