J'ai besoin d'aide pour formuler cette requête. J'ai deux tables (pertinentes), que je videra ici pour référence:Requête MySQL: Obtenir toutes les pochettes d'album
CREATE TABLE IF NOT EXISTS `albums` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL COMMENT 'owns all photos in album',
`parent_id` int(11) DEFAULT NULL,
`left_val` int(11) NOT NULL,
`right_val` int(11) NOT NULL,
`name` varchar(80) COLLATE utf8_unicode_ci NOT NULL,
`num_photos` int(11) NOT NULL,
`date_created` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `parent_id` (`parent_id`,`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;
CREATE TABLE IF NOT EXISTS `photos` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`album_id` int(11) NOT NULL,
`filename` varchar(32) COLLATE utf8_unicode_ci NOT NULL,
`num_views` int(11) NOT NULL DEFAULT '0',
`date_uploaded` int(11) NOT NULL,
`visibility` enum('friends','selected','private') COLLATE utf8_unicode_ci NOT NULL,
`position` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=26 ;
Maintenant, je veux saisir la première photo (position la plus basse #) de chaque album appartenant à un utilisateur.
Voici ce que je suis en train:
SELECT * FROM albums JOIN photos ON photos.album_id=albums.id WHERE albums.user_id=%s GROUP BY album_id HAVING min(position)
Mais la clause having ne semble pas avoir un effet. Quel est le problème?
Il me semble maintenant que min (position) est probablement juste de revenir un certain nombre. Cela a du sens en anglais, mais pas en tant que question. Mais 'position = min (position)' ne fonctionne pas non plus, et je n'ai pas de chance avec ORDERing non plus. – mpen