0

我有以下查询:

SELECT a.*,
ps4_media.filename,
ps4_galleries.name as galleryname,
ps4_media_iptc.description,
ps4_media_iptc.title,
ps4_media_iptc.headline,
ps4_media.date_added,
ps4_galleries.created,
ps4_folders.name as foldername,
ps4_galleries.gallery_count 
FROM ps4_media_galleries a 
INNER JOIN (SELECT ps4_media_galleries.gallery_id,
min(ps4_media_galleries.gmedia_id) AS minID 
FROM ps4_media_galleries GROUP BY ps4_media_galleries.gallery_id) b 
ON a.gallery_id = b.gallery_id 
AND a.gmedia_id = b.minID
INNER JOIN ps4_media ON ps4_media.media_id = a.gmedia_id 
INNER JOIN ps4_folders ON ps4_folders.folder_id = ps4_media.folder_id
INNER JOIN ps4_galleries ON ps4_galleries.gallery_id = a.gallery_id
INNER JOIN ps4_media_iptc ON ps4_media_iptc.media_id = a.gmedia_id
ORDER BY ps4_galleries.created DESC

如何让 ps4_media.filename 随机,其他一切都很好,只需要 ps4_media.filename 的缩略图和搜索结果中的记录。

4

2 回答 2

0

这个答案是基于对您的表设计的大量假设- 因为您的问题中没有说明架构或数据流 hierarch。

我举了一个例子来说明你需要对你的查询做些什么。如您所见,此设计是一个平滑的设计,您可能有许多交叉引用等。因此,我再次重复自己说,通过展示您的模式来帮助您来帮助我们是至关重要的。

-- image belongs to a media 
-- folder has many images
---- folder belongs to a media
-- gallery has many media

表设计:

-- Gallery Table
GID     GNAME       GLOCATION
1       art         cambridge
2       art         torronto
3       picture     melbourne
4       sculpture   paris

-- Media Table
MID     MNAME   MGID
10      screen  1
15      brail   2
20      embose  1
25      print   3
30      print   4
35      embose  2

-- Folder Table
FID     FNAME       FMID
200     fprojToro1  15
201     fprojMelb1  25
202     fprojCamb1  20
203     fprojCamb2  20
204     fprojToro2  15
205     fprojToro3  35
206     fprojMelb2  25
207     fprojMelb3  25

-- Image Table
IID     INAME   IFID
1001    tb01    200
1002    mp02    201
1005    te05    205
1008    te08    205
1010    mp10    206
1015    mp15    207
1018    te18    205

查询一:

-- 生成随机文件名

select iname from image
order by Rand()
limit 1
;

结果1:

INAME
tb01

查询 2:

-- 因为我使用了左连接 -- 并且一些图像属于同一个文件夹 -- 在以下查询中可能会获得超过 -- 1 条记录

select g.*, m.mname, f.fname, i.iname
from gallery g
left join media m
on m.mgid = g.gid
left join folder f
on f.fmid = m.mid
left join image i
on i.ifid = f.fid
where i.iname = (select iname from image
                 order by Rand()
                 limit 1)
;

结果 2:

GID     GNAME   GLOCATION   MNAME   FNAME        INAME
3       picture     melbourne   print   fprojMelb2  mp10

查询 3:

-- 再次将主查询限制为 1

select g.*, m.mname, f.fname, i.iname
from gallery g
left join media m
on m.mgid = g.gid
left join folder f
on f.fmid = m.mid
left join image i
on i.ifid = f.fid
where i.iname = (select iname from image
                 order by Rand()
                 limit 1)
limit 1
;

结果 3:

GID     GNAME   GLOCATION   MNAME   FNAME       INAME
2       art     torronto    embose  fprojToro3  te18
于 2012-12-05T10:28:54.600 回答
0

您需要通过RAND() Try this::

SELECT a.*,
ps4_media.filename,
ps4_galleries.name as galleryname,
ps4_media_iptc.description,
ps4_media_iptc.title,
ps4_media_iptc.headline,
ps4_media.date_added,
ps4_galleries.created,
ps4_folders.name as foldername,
ps4_galleries.gallery_count FROM ps4_media_galleries a INNER JOIN (SELECT ps4_media_galleries.gallery_id,
min(ps4_media_galleries.gmedia_id) AS minID FROM ps4_media_galleries GROUP BY ps4_media_galleries.gallery_id) b ON a.gallery_id = b.gallery_id AND a.gmedia_id = b.minID
INNER JOIN ps4_media ON ps4_media.media_id = a.gmedia_id INNER JOIN ps4_folders ON ps4_folders.folder_id = ps4_media.folder_id
INNER JOIN ps4_galleries ON ps4_galleries.gallery_id = a.gallery_id
INNER JOIN ps4_media_iptc ON ps4_media_iptc.media_id = a.gmedia_id
ORDER BY RAND()
于 2012-12-05T05:01:39.607 回答