简体   繁体   中英

Get Records based on a Random value for a specific column/field - MySQL

I have the fillowing Query:

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

How do I get ps4_media.filename to be random, everything else is fine just want the thumbnail which ps4_media.filename is to be and of the records in the found set.

You need to order the selection by 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()

This answer is based on a big-huge-loads of assumptions on your table design - since the schema or data flow hirerarch is not stated in your question.

I have taken an example to illustrate what you need to do on your query. As you see this design is a smooth design, where you may have many cross references and so on. So I am repeating myself again to say that it's crucial you help us by showing your schema to help you.

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

Table Design:

-- 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

Query 1:

-- generate a random file name

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

Results 1:

INAME
tb01

Query 2:

-- since i have used left join -- and some images belong to the same folder -- there is a possibility of getting more than -- 1 record in the following query

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)
;

Results 2:

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

Query 3:

-- limit the main query by 1 again

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
;

Results 3:

GID     GNAME   GLOCATION   MNAME   FNAME       INAME
2       art     torronto    embose  fprojToro3  te18

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM