SQL查询没有排序desc

时间:2012-04-09 11:34:47

标签: sql sqlite

这个sql命令在sqlitemanager中工作正常 但在我的Android应用程序中,这不是排序desc ...

select t._id,  u.name, c.commdate, c.message
from tickets t, users u, comments c
where c.userid = u._id and c.ticketid = t._id
and t.status = 5
group by t._id
having max(c.commdate)
order by c.commdate desc

1 个答案:

答案 0 :(得分:0)

我感觉“有”部分是一个问题。尝试(未经测试);

SELECT t._id, u.name, c.commdate, c.message
FROM tickets t
JOIN comments c ON (t._id = c.ticketid)
-- Find most decent comments on tickets, but handle lack of any comments
LEFT JOIN (
    SELECT c2.ticketid, MAX(c2.commdate) as max_commdate
    FROM comments c2
) AS latest ON (c.ticketid = latest.ticketid AND c.commdate = latest.max_commdate)
JOIN users u ON (c.userid = i._id)
WHERE t.status = 5
ORDER BY t._id DESC, c.commdate DESC