从 mysql 表中选择最新的
我有两个 mysql 表:
包含可以购买的物品的项目:
CREATE TABLE `item` (
`itemid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`itemid`)
) ENGINE=InnoDB;
包含所有购买的购买:
CREATE TABLE `purchase` (
`purchaseid` int(11) NOT NULL AUTO_INCREMENT,
`date` date DEFAULT NULL,
`amount` int(11) DEFAULT NULL,
`itemid` int(11) DEFAULT NULL,
PRIMARY KEY (`purchaseid`)
) ENGINE=InnoDB;
我想根据日期和购买 ID 选择最近的 20 个购买,并加入项目表以显示这些购买的名称。如果某个商品在最近 20 次购买中被购买了多次,则该商品应该只显示一次。没有重复项。我真的无法弄清楚这一点..也许你可以?谢谢!
I have two mysql tables:
Item containing items that one can buy:
CREATE TABLE `item` (
`itemid` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
PRIMARY KEY (`itemid`)
) ENGINE=InnoDB;
Purchase containing all purchases:
CREATE TABLE `purchase` (
`purchaseid` int(11) NOT NULL AUTO_INCREMENT,
`date` date DEFAULT NULL,
`amount` int(11) DEFAULT NULL,
`itemid` int(11) DEFAULT NULL,
PRIMARY KEY (`purchaseid`)
) ENGINE=InnoDB;
I want to select the most 20 recent purchases based on date and purchaseid and join the item table to show the name of these purchases. If an item has been purchased more than once in the 20 recent purchases it should only show up once. No duplicates. I really can't figure this out.. Maybe you can? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
难道不是:
OR
使用
DISTINCT
允许您省略重复项,GROUP BY
也是如此(它还允许您对分组数据执行函数)Wouldn't it be:
OR
Using
DISTINCT
allows you to omit duplicates, as doesGROUP BY
(which also allows you to perform functions on the grouped data)