例子:
我希望你明白。我正在尝试使用此查询,但它仅返回用户 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
尝试:
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
询问:
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)
我会尝试类似的东西
SELECT MAX(id), idto, idfrom
FROM messages
WHERE idto = 1 OR idfrom = 1
GROUP BY idto, idfrom
它可能比你需要的多一点,但它很好而且清晰。
尝试:
SELECT idMsg, idFrom, idTo
FROM Messages
WHERE idFrom = 1 OR idTo = 1
GROUP BY idFrom
ORDER BY visualized DESC