加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite学习笔记

发布时间:2020-12-13 00:01:33 所属栏目:百科 来源:网络整理
导读:在一个开源项目中看到的一个SQLite查询语句,看得我一头雾水,经过一天的琢磨, 终于解开谜团,源码和注解如下: public Cursor getListing() { return dbAndTripLog.rawQuery("select t.id as _id,t.id||' - '||start_date as cmt," + "count(tl.id)||' pos

在一个开源项目中看到的一个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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读