下午的人,
我有一个很好的挖掘,无法找到答案,所以是个好时机!
我想从一个表中选择随机行,然后从另一个表中加入这个随机行,其中我拥有的ID是相同的。仅选择第二个表中的条目位置也很棒。我已经尝试过各种各样的子查询但是有点迷失了。读取的内部联接将执行此操作,但同样具有随机性!哎呀...
SELECT
tracks.track_id,
cuttings.square_cutting,
cuttings.cutting_2,
cuttings.cutting_3,
cuttings.blog_text
FROM tbl_tracks tracks,
(SELECT
square_cutting,
cutting_2,
cutting_3,
blog_text
FROM
tbl_cuttings
WHERE track_id = tracks.track_id <-- wont find it, obviously!!
ORDER BY RAND()
LIMIT 1) cuttings
WHERE tracks.active = '1' ORDER BY RAND()
提前感谢您的帮助。
所以: 我喜欢随机曲目显示 跟踪ID - &gt;随机切割,其中可能有很多,但我只想要1。
如果存在与该轨道相关的切割,那么仅显示结果将是理想的。
希望有所帮助。
我现在正试图更进一步,并通过RAND()种子订购,因为我现在不得不加入分页。唯一的问题是由于给定的种子,它没有给我相同的随机列表。任何想法?
SELECT
tracks.track_id,
cuttings.square_cutting,
cuttings.cutting_2,
cuttings.cutting_3,
cuttings.blog_text
FROM tbl_tracks tracks
INNER JOIN
(SELECT track_id,
square_cutting,
cutting_2,
cutting_3,
blog_text
FROM
tbl_cuttings
ORDER BY RAND()) cuttings ON tracks.track_id = cuttings.track_id
WHERE tracks.active = '1'
ORDER BY RAND(1)
LIMIT 0,4;
答案 0 :(得分:2)
你可以使用内连接
SELECT
tracks.track_id,
cuttings.square_cutting,
cuttings.cutting_2,
cuttings.cutting_3,
cuttings.blog_text
FROM tbl_tracks tracks
INNER JOIN
(SELECT track_id,
square_cutting,
cutting_2,
cutting_3,
blog_text
FROM
tbl_cuttings
ORDER BY RAND()
LIMIT 1) cuttings on cuttings.track_id = tracks.track_id
WHERE tracks.active = '1'
ORDER BY RAND()