以为我有这个破解但似乎并没有起作用。尝试根据登录的用户名($ user)以及其他随机图片网址(不等于$ user)显示一个特定的图片网址。
当页面刷新时,图像存在,但并非总是如此。我需要始终基于$ user显示图像。任何想法如何我总是包括基于$ user的记录?所有记录必须按随机顺序排列。使用UNION会更好吗?
$photo=mysql_query("SELECT A. * FROM (
SELECT DISTINCT * FROM images
WHERE approved ='Y'
ORDER BY (username = '".$user."') DESC, RAND()
LIMIT 10)
as A ORDER BY RAND()");
尝试了以下UNION,但似乎仍然无法做到:
(SELECT *
FROM images
WHERE username = '".$user."' )
UNION ALL
(SELECT *
FROM (SELECT *
FROM images
WHERE username != '".$user."'
ORDER BY Rand()
LIMIT 9) `t`
ORDER BY Rand())
UNION似乎总是排除$ user
当我尝试:
$photo=mysql_query("(SELECT *
FROM images
WHERE username <> '".$user."' and approved = 'y' ORDER BY Rand() LIMIT
10)
UNION ALL
(SELECT *
FROM (SELECT *
FROM images
WHERE username = '".$user."' and approved = 'y'
ORDER BY Rand()
LIMIT 1) `t` )");
我得到$ user但它始终是最后一张图片。并且只返回了9张图片。每次都需要随机排序。我究竟做错了什么?
答案 0 :(得分:0)
如果您想要来自用户的所有图像,则可以执行以下操作:
android:defaultValue="0x0A"
如果您想要来自用户的一个图像,则您的select i.*
from images i
order by (i.username = $user) desc, rand()
limit 10;
查询应该有效。如果您想要所需的用户,请使用子查询和相同的union all
子句:
order by
如果你有很多图像(比如数千或更多),那么select i.*
from ((select i.*
from images i
where i.username = $user
order by rand()
limit 1
) union all
(select i.*
from images i
where i.username <> $user
order by rand()
limit 9
)
) i
order by (i.username = $user) desc, rand();
将会变得昂贵,你应该研究其他方法来获取你想要的东西。