Web SQL从第二个表获取最后一条记录

时间:2015-06-11 12:25:56

标签: sql web-sql

我有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

它不能正常工作

1 个答案:

答案 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