via une requête, je fais un listing d'annonces enregistrées dans la base. Et j'aimerais également récupérer une photo liée à cette annonce, et plus précisément celle dont le champ photo_position est le plus petit.
Voici les 2 tables en question :
Code : Tout sélectionner
CREATE TABLE `annonces` (
`annonce_id` int(11) NOT NULL auto_increment,
`annonce_user_id` int(11) NOT NULL
PRIMARY KEY (`annonce_id`),
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=530 ;
CREATE TABLE `photos` (
`photo_id` int(11) NOT NULL auto_increment,
`photo_annonce_id` int(11) NOT NULL,
`photo_datetime` datetime NOT NULL,
`photo_main` tinyint(4) NOT NULL,
`photo_position` tinyint(4) NOT NULL,
PRIMARY KEY (`photo_id`),
KEY `photo_annonce_id` (`photo_annonce_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1097 ;$sql = "SELECT annonce_id, annonce_price, annonce_description, annonce_old_price, photo_id, annonce_type_bien, annonce_rooms, annonce_zipcode, annonce_city,
annonce_surface, annonce_vendu
FROM ".$cfg_prefixe."annonces
LEFT JOIN ".$cfg_prefixe."photos ON (photo_annonce_id = annonce_id)
WHERE annonce_statut = 3";
Ce que je voudrais ajouter, c'est quelque chose du genre Code : Tout sélectionner
ORDER BY photo_position LIMIT 1Comment puis-je faire ?