我有以下查询:
SELECT picture.id, picture.lowresimageurl, picture.medresimageurl, picture.highresimageurl, picture.caption, picture.numberoflikes, picture.numberofdislikes
FROM apps_instagram_picture_category category
INNER JOIN apps_instagram_shop_picture picture ON category.picture_id = picture.id
INNER JOIN apps_instagram_shop shop ON shop.id = picture.shop_id
WHERE category.first_level_category_id = ?
AND picture.deletedAt IS NULL
AND picture.isLocked = 0
AND shop.deletedAt IS NULL
AND shop.isLocked = 0
AND shop.expirydate IS NOT NULL
AND shop.expirydate > ?
AND shop.owner_id IS NOT NULL
GROUP BY shop.id
LIMIT ?
OFFSET ?
我想从一个独特的商店获得一张具有特定类别X的随机图片。该查询的问题在于它总是选择它在数据库中找到的第一张图片,因此它不是随机的。关于如何让它更随机的任何想法?我实际上也想让商店随机,我可以通过在查询中给出随机偏移来做到这一点,但我不知道如何获得随机图片。
答案 0 :(得分:-2)
在您的查询中使用ORDER BY RAND()
,例如
SELECT picture.id, picture.lowresimageurl, picture.medresimageurl, picture.highresimageurl, picture.caption, picture.numberoflikes, picture.numberofdislikes
FROM apps_instagram_shop_picture picture
INNER JOIN apps_instagram_picture_category category ON category.picture_id = picture.id
INNER JOIN apps_instagram_shop shop ON shop.id = picture.shop_id
WHERE category.first_level_category_id = ?
AND picture.deletedAt IS NULL
AND picture.isLocked = 0
AND shop.deletedAt IS NULL
AND shop.isLocked = 0
AND shop.expirydate IS NOT NULL
AND shop.expirydate > ?
AND shop.owner_id IS NOT NULL
GROUP BY shop.id
ORDER BY shop.?, RAND()
LIMIT ?
OFFSET ?