Web SQL get last record from second table

232 views Asked by At

I have 2 tables like that

ChatList Table COLUMNS : ID INTEGER PRIMARY KEY, PHONE, DISPLAYNAME, SESSIONID

Messages Table COLUMNS : ID INTEGER PRIMARY KEY, [DATETIME], [FROM], [TO], [TYPE], [MESSAGE], [ISGROUP], [GROUPID], [READED], [SESSIONID], [MESSAGEID], [DISPLAYNAME]

I want get last record from messages table to chatlist join

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

It's not working

1

There are 1 answers

4
Sari Rahal On

You forgot to add the 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