我需要按时间获得结果顺序,但是因为我使用sql查询到其他sql查询的一段时间我得到rtoken = token[0:1]
$arrID
结果是:
$req1==mysql_query("SELECT friendId FROM friends ");
while($res1=mysql_fetch_row($req1)) {
$arrID = $res['friendId'];
$req = mysql_query("SELECT * FROM posts WHERE postby=$arrID ORDER BY posttime DESC ");
while ($res = mysql_fetch_row($req)) {
echo $res['bostby'];
echo $res['postcontent'];
}
}
并且我需要按时间顺序而不是id
post 1 by 2 content aaaaaaaa time: 11:60
post 2 by 2 content bbbbbbbb time 11:59
post 3 by 2 content aaaaaaaa time: 11:58
post 4 by 2 content bbbbbbbb time 11:56
post 5 by 3 content aaaaaaaa time: 11:61
post 6 by 3 content bbbbbbbb time 11:60
post 7 by 3 content aaaaaaaa time: 11:59
post 8 by 5 content bbbbbbbb time 12:20
post 8 by 5 content bbbbbbbb time 12:19
答案 0 :(得分:1)
使用连接两个表的单个查询,而不是执行嵌套循环。
SELECT p.* FROM posts AS p
JOIN friends AS f ON p.postby = f.friendId
ORDER BY p.posttime DESC
从您的评论中看来,您的第一个查询并非如此简单。但是你可以加入一个子查询。
SELECT p.* FROM posts AS p
JOIN (
SELECT user_from AS friendId
FROM friend_requests
WHERE user_to = $SessionID AND accepted = '1'
UNION
SELECT user_to AS friendId
FROM friend_requests
WHERE user_from = $SessionID AND accepted = '1'
) AS f ON p.postby = f.friendId
ORDER BY p.posttime DESC