-2

例子:

消息

我希望你明白。我正在尝试使用此查询,但它仅返回用户 1 发送的查询。我还需要发送给他的查询。但没有重复相同的参与者。

4

5 回答 5

1
SELECT idMsg(Max), idFrom, idTo
FROM Messages
WHERE idFrom = 1 OR idTo = 1
GROUP BY idFrom
ORDER BY visualized DESC

或者

SELECT top 1 idMsg, idFrom, idTo
FROM Messages
WHERE idFrom = 1 OR idTo = 1
ORDER BY DESC
于 2013-05-02T05:35:38.227 回答
0

尝试:

select case when idfrom < idto then idfrom else idto end userid1,
       case when idfrom > idto then idfrom else idto end userid2,
       max(id) id
from messages
where 1 in (idfrom, idto)
group by
       case when idfrom < idto then idfrom else idto end,
       case when idfrom > idto then idfrom else idto end
于 2013-05-02T05:35:19.727 回答
0

询问:

SELECT m1.idMsg, m1.idFrom, m1.idTo
FROM Messages m1
WHERE m1.dataMsg = (SELECT MAX(m2.dataMsg)
                    FROM Messages m2
                    WHERE m2.idFrom = m1.idFrom
                    AND m2.idTo = m1.idTo) 
于 2013-05-02T05:39:02.633 回答
0

我会尝试类似的东西

SELECT MAX(id), idto, idfrom
FROM messages
WHERE idto = 1 OR idfrom = 1
GROUP BY idto, idfrom

它可能比你需要的多一点,但它很好而且清晰。

于 2013-05-02T05:25:03.350 回答
0

尝试:

SELECT idMsg, idFrom, idTo
FROM Messages
WHERE idFrom = 1 OR idTo = 1
GROUP BY idFrom
ORDER BY visualized DESC

于 2013-05-02T05:14:17.103 回答