gpt4 book ai didi

MySQL查询: Get all album covers

转载 作者:行者123 更新时间:2023-11-29 15:10:03 25 4
gpt4 key购买 nike

我需要一些帮助来制定此查询。我有两个(相关的)表,我将在此处转储它们以供引用:

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 ;

现在我想从某个用户拥有的每个相册中获取第一张照片(最低位置#)。

这就是我正在尝试的:

SELECT * FROM albums JOIN photos ON photos.album_id=albums.id WHERE albums.user_id=%s GROUP BY album_id HAVING min(position)

但是having子句似乎没有效果。有什么问题吗?

最佳答案

select * from album, photos 
where album_id=albums.id
and albums.user_id='user_id'
and photos.id = (select id from photos where
album_id = album.id order by position LIMIT 1)

关于MySQL查询: Get all album covers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/984493/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com