SQLite学习笔记

来源:互联网 发布:js数组指定下标截取 编辑:程序博客网 时间:2024/06/03 19:24

在一个开源项目中看到的一个SQLite查询语句,看得我一头雾水,经过一天的琢磨,

终于解开谜团,源码和注解如下:

    public Cursor getListing() {    return dbAndTripLog.rawQuery("select t.id as _id,t.id||' - '||start_date as cmt," +    "count(tl.id)||' positions,'||coalesce(trip_meter,'?')||' meters'||','||" +    "coalesce((trip_duration/1000),'?')||' seconds' as descr from Trips t " +    "left outer join TripsLogs tl on (t.id=tl.tripId) group" +    " by t.id,start_date order by start_date desc",null);//as "作为"的意思,能把几列查询结果合并成一个新列 如:t.id||' - '||start_date as cmt// count(x)函数返回在同一组内,x字段中值不等于NULL的行数。count(*)函数返回在同一组内的数据行数。//coalesce(X,Y,...)返回函数参数中第一个非NULL的参数,如果参数都是NULL,则返回NULL。该函数至少2个参数。//Trips t left outer join TripsLogs tl on (t.id=tl.tripId)两个表格根据键值合并成一个表格,//查询后的结果,Trip表格中的的列会在左边显示//group by 按指定的组显示查询结果,指定组有几个不同的值,就有几行//order by 按指定组排列//desc 降序排列,asc 升序排列//"||"用于连接字符串        }

参考文旦:

http://sqlite.awardspace.info/syntax/sqlitepg06.htm

http://www.cnblogs.com/stephen-liu74/archive/2012/01/13/2322027.html

原创粉丝点击