sql - MySQL left join with right table having order and limit -
i have table of itineraries , table of associated images foreign key. i'd list of itineraries first image, ordering itineraries_images sort limit of 1
create table itineraries ( id int(10) auto_increment, is_live tinyint(1), title varchar(255), body text, primary key (id) ) create table itineraries_images ( id int(10) auto_increment, itineraries_id int(10), is_live tinyint(1), caption varchar(255), image_src varchar(255), sort smallint(5), primary key (id), key itineraries_id (itineraries_id) )
i'm doing left join, doesn't sort joined table
select i.*, ii.image_src, ii.caption itineraries left outer join itineraries_images ii on i.id=ii.itineraries_id , ii.is_live=1 i.is_live=1 group ii.itineraries_id order i.id, ii.sort
been looking @ subqueries... still can't working :(
many thanks,
rob.
this job , give latest image_src definitions shows id in images table auto_increment
order ii.id desc
in group_concat group images in descending order , using substring_index
give latest image
select i.*, substring_index(group_concat( ii.image_src order ii.id desc ),',',1) image_src , substring_index(group_concat( ii.caption order ii.id desc ),',',1) caption itineraries left outer join itineraries_images ii on i.id=ii.itineraries_id , ii.is_live=1 i.is_live=1 group i.id order i.id, ii.sort
Comments
Post a Comment