Select most recent from mysql table -


i have 2 mysql tables:

item containing items 1 can buy:

create table `item` ( `itemid` int(11) not null auto_increment, `name` varchar(50) default null, primary key (`itemid`) ) engine=innodb; 

purchase containing 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 select 20 recent purchases based on date , purchaseid , join item table show name of these purchases. if item has been purchased more once in 20 recent purchases should show once. no duplicates. can't figure out.. maybe can? thanks!

wouldn't be:

select `name` `item` join `purchase` using(`itemid`) group `itemid` order `date` desc limit 20 

or

select distinct `name` `item` join `purchase` using(`itemid`) order `date` desc limit 20 

using distinct allows omit duplicates, group by (which allows perform functions on grouped data)


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -