0

这是我的查询:

mysql_query("SELECT userid FROM messages WHERE userid<>'$myid' AND friendid='$myid' AND status=1 UNION SELECT friendid FROM messages WHERE userid='$myid' AND friendid<>'$myid' AND status=1");

我想添加分页,所以我需要限制我发消息的朋友数量。但是每当我添加 LIMIT 时,它都会显示错误。另外我想先显示最新消息ORDER BY time DESC,但这也不起作用。帮助 ?

4

2 回答 2

6

将该查询包含在子查询和ORDER BY ... LIMIT...外部查询中:

SELECT *
FROM
(
    SELECT userid 
    FROM messages 
    WHERE userid   <> '$myid' 
      AND friendid = '$myid' 
      AND status = 1 
    UNION 
    SELECT friendid 
    FROM messages
    WHERE userid = '$myid' 
      AND friendid <> '$myid' 
      AND status = 1
) AS sub
ORDER BY ...
LIMIT ...;
于 2013-03-19T11:21:40.370 回答
1

试试吧

SELECT DISTINCT userid FROM...
于 2013-03-19T11:19:50.043 回答