如何使用Android 2.1中的join子句中的选择?

时间:2010-10-31 23:09:25

标签: android sqlite greatest-n-per-group

我有一个查询做了一些神奇的排序组加入,在Android 2.2(froyo)版本的SQLite上运行得很好:

SELECT channels.*, p.*
FROM channels 
LEFT JOIN programs p ON channels._id = p.channel_id
JOIN (select p2.channel_id, max(p2.airdate) max_air FROM programs p2 
      GROUP BY p2.channel_id) pj
ON pj.channel_id = p.channel_id AND pj.max_air = p.airdate
ORDER BY channels.channel_index asc;

由于某种原因,该连接不适用于Android 2.1,但失败并带有

no such column: pj.channel_id

我认为这是因为SQLite已经从2.1升级到2.2,但适用于这两个版本的SQL是什么?

1 个答案:

答案 0 :(得分:0)

分辨率很简单,我只需要删除别名“p”:

SELECT channels.*, programs.*
FROM channels
LEFT JOIN programs ON channels._id = programs.channel_id
JOIN (select p2.channel_id channel_id, max(p2.airdate) max_air FROM programs p2
      GROUP BY p2.channel_id) pj
ON pj.channel_id = programs.channel_id AND pj.max_air = programs.airdate