我有2张这样的表
聊天列表表 COLUMNS:ID INTEGER PRIMARY KEY,PHONE,DISPLAYNAME,SESSIONID
消息表 COLUMNS:ID INTEGER PRIMARY KEY,[DATETIME],[FROM],[TO],[TYPE],[MESSAGE],[ISGROUP],[GROUPID],[READED],[SESSIONID],[MESSAGEID],[DISPLAYNAME]
我想从消息表到聊天列表加入
获取最后一条记录SELECT m.ID as MID, m.DATETIME, c.ID, c.DISPLAYNAME, c.PHONE, c.SESSIONID, m.TYPE, m.MESSAGE
FROM [CHATLIST] as c
, [MESSAGES] as m
WHERE m.SESSIONID = c.SESSIONID
GROUP BY c.SESSIONID
ORDER BY MID DESC
它不能正常工作
答案 0 :(得分:0)
您忘记添加LIMIT
SELECT m.ID as MID, m.DATETIME, c.ID, c.DISPLAYNAME, c.PHONE, c.SESSIONID,
m.TYPE, m.MESSAGE
FROM [CHATLIST] as c, [MESSAGES] as m
WHERE m.SESSIONID = c.SESSIONID
GROUP BY c.SESSIONID
ORDER BY MID DESC
LIMIT 1