gpt4 book ai didi

mysql - 如何从数据库中获取唯一记录?

转载 作者:行者123 更新时间:2023-11-30 23:23:32 24 4
gpt4 key购买 nike

这是我的表结构

album
id ---- albumname
1 General
3 Sports
9 Activities
gallery
id --- ablumid--- image_thumb -- image_full
1 1 uploads/1358401616.jpg uploads/thumbs/thumb_1358401616.jpg
2 1 uploads/1358401641.jpg uploads/thumbs/thumb_1358401641.jpg
3 1 uploads/1358402071.jpg uploads/thumbs/thumb_1358402071.jpg
4 3 uploads/1358402334.jpg uploads/thumbs/thumb_1358402334.jpg

问题是我有太多的图片,一个画廊都放不下。

这是我的mysql查询

SELECT DISTINCT gl.catid as gallery_cat_id, ab.albumname,gl.imagest 
FROM albums as ab
INNER JOIN gallery as gl ON ab.id=gl.catid
order by ab.albumname ASC

这是结果

gallery_id  albumname   image_full
38 Activities uploads/thumbs/thumb_1358424428.jpg
41 Activities uploads/thumbs/thumb_1358424483.jpg
40 Activities uploads/thumbs/thumb_1358424468.jpg
39 Activities uploads/thumbs/thumb_1358424446.jpg

查询显示我在查询中添加 DISTINCT 的图库表中的所有记录,但它不起作用。谁能告诉我我在这里错过了什么?

最佳答案

如果每个相册只需要一张图片(不管是什么),您可以使用 GROUP BY 子句,按相册 ID 分组:

SELECT gl.catid as gallery_cat_id, ab.albumname, gl.imagest 
FROM
albums as ab
INNER JOIN gallery as gl
ON ab.id=gl.catid
GROUP BY ab.id
order by ab.albumname ASC

关于mysql - 如何从数据库中获取唯一记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14443181/

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