我在mysql数据库中有3个表
chat
user1, user2, chat_key
chat_seen
msgid, userid, viewed
users
username, status, key
所以我试图显示用户在线排序聊天然后看到的消息。但它不起作用。这就是我到目前为止,iv只是让它与聊天和用户一起工作,我不知道如何通过聊天添加订单
("SELECT u.random, u.status, c.chat_key, c.seen, u.username
FROM chat c, users u
WHERE CASE
WHEN c.user1 = ?
THEN c.user2 = u.random
WHEN c.user2 = ?
THEN c.user1= u.random
END
AND (
c.user1 = ?
OR c.user2 = ?)
ORDER BY u.status DESC LIMIT 50");
所以结果应该是
username - online - (2 messages)
username - online - (1 message)
username - offline - (20 messages)
username - offline - (2 messages)
username - offline - (o messages)
目前我
username - online - (0 messages)
username - online - (0 messages)
username - offline - (2messages)
username - offline - 20 messages)
答案 0 :(得分:0)
请尝试以下查询:
("SELECT
u.random,
u.status,
c.chat_key,
c.seen,
u.username
FROM chat c, users u
WHERE
(c.user1 = ? AND c.user2 = u.random)
OR
(c.user2 = ? AND c.user1 = u.random)
AND
(c.user1 = ? OR c.user2 = ?)
ORDER BY u.status DESC LIMIT 50");
让我知道它是否有效。 :d