mysql - Select only the last inserted item related to another registry -
i modeled small database easier explanation:
create table bands ( id integer unsigned not null auto_increment, name varchar(120) null, primary key(id) ) type=innodb; create table albums ( id integer unsigned not null auto_increment, band_id integer unsigned not null, album_name varchar(120) null, rating integer unsigned null, insertion_date timestamp null, primary key(id), index albums_fkindex1(band_id), foreign key(band_id) references bands(id) on delete no action on update no action ) type=innodb;
now, pretending have bands , many albums registered in respective tables, want select last inserted album each registered band.
ps: have use "album.insertion_date" field determine album last inserted.
try joining 2 tables , filtering insertion_date , band:
select al.* albums al inner join bands b on al.band_id=b.id al.insertion_date=( select max(insertion_date) albums band_id=b.id )
Comments
Post a Comment