简体   繁体   English

WebSQL / SQLite按JavaScript日期obj排序

[英]WebSQL/SQLite order by javascript date obj

I'm working on a html5/jqm/phonegap application. 我正在研究html5 / jqm / phonegap应用程序。 I'm selecting a list of records from a client-side db table. 我正在从客户端数据库表中选择记录列表。 I want to group and order them by date but the date that exists in the table is a JS date object like: 我想按日期对它们进行分组和排序,但是表中存在的日期是一个JS日期对象,例如:

Mon Nov 19 2012 16:20:55 GMT+0000 (GMT)

I want to group and order them by the numerical day (18,19,20 in this example). 我想按数字天(在此示例中为18、19、20)对它们进行分组和排序。 So the following details: 因此,以下详细信息:

Sun Nov 18 2012 16:20:55 GMT+0000 (GMT)
Mon Nov 19 2012 16:20:55 GMT+0000 (GMT)
Sun Nov 18 2012 16:20:55 GMT+0000 (GMT)
Tue Nov 12 2012 16:20:55 GMT+0000 (GMT)

Would be returned in this order: 将按以下顺序返回:

Tue Nov 20 2012 16:20:55 GMT+0000 (GMT)
Mon Nov 19 2012 16:20:55 GMT+0000 (GMT)
Sun Nov 18 2012 16:20:55 GMT+0000 (GMT)
Sun Nov 18 2012 16:20:55 GMT+0000 (GMT)

Is it possible to do this in sql as part of the select? 作为选择的一部分,是否可以在sql中执行此操作? Or if not could someone piont me in the right direction on how to do this within Js/jQuery? 或者,如果不能,那么有人会朝着正确的方向指点我,说明如何在Js / jQuery中做到这一点?

Thanks in advance 提前致谢

** Edit in response to questions from Pete ** **根据彼得的问题进行编辑**

I'm using the Jaascript/html5 client side version of SQL, which I believe is based on SQLite. 我正在使用Jaascript / html5客户端版本的SQL,我相信它基于SQLite。 To create the DB I'm just calling the following js - 要创建数据库,我只是调用以下js-

var db = openDatabase(shortName, version, displayName, maxSize);

My Db schema is : 我的Db模式是:

transaction.executeSql('CREATE TABLE RECORDINGS (id INTEGER PRIMARY KEY AUTOINCREMENT, caseRef, startDate, endDate, caseTypeId, actionTypeId, notes, userId, syncStatus);');
transaction.executeSql('INSERT INTO RECORDINGS (caseRef, startDate, endDate, caseTypeId, actionTypeId, notes, userId, syncStatus) VALUES ("CR1234", "'+startDate+'", "'+endDate+'", 1, 2, "Meeting about something", 1, 0);');

My select currently looks like: 我的选择当前看起来像:

function getRecordingsQuery(transaction) {
        transaction.executeSql('SELECT RECORDINGS.id, RECORDINGS.caseRef, RECORDINGS.startDate, RECORDINGS.endDate, RECORDINGS.notes, RECORDINGS.syncStatus, CASETYPES.description FROM RECORDINGS, CASETYPES WHERE RECORDINGS.caseTypeId = CASETYPES.id AND RECORDINGS.userId =? ORDER BY startDate DESC', [window.currentSignedInUserId], getRecordingsDataHandler, errorHandler);
    }

Does that help? 有帮助吗?

UPDATE 更新

SQLLite does not support DATEPART , but it does support strftime . SQLLite不支持DATEPART ,但是它支持strftime Try adding the following as your ORDER BY : 尝试将以下内容添加为您的ORDER BY

ORDER BY CAST(strftime("%d", date) AS INTEGER) DESC

You could instead process the sort in JavaScript. 您可以改为使用JavaScript处理排序。 I'm sure there are better, or more efficient ways to do this, but you could just use the built-in sort for arrays like so: 我敢肯定有更好或更有效的方法可以做到这一点,但是您可以对数组使用内置排序,如下所示:

var getRecordingsDataHandler = function getRecordingsDataHandler(tx, results) {
    var rows = [],
        row = null,
        i = 0;
      if (results.rows && results.rows.length) {
        for (i = 0; i < results.rows.length; i += 1) {
            rows.push({
                "startDate": new Date(results.rows.item(i).startDate),
                "data": results.rows.item(i)
            });
        }
        rows.sort(function (a, b) {
            var ascending = a.startDate.getDate() - b.startDate.getDate(),
                descending = b.startDate.getDate() - a.startDate.getDate();
            return descending;
        });
        for (i = 0; i < rows.length; i += 1) {
            row = rows[i].data;
            //process the rest
            //since row === results.rows.item(i), reference from row
        }
    }
};

ORIGINAL: 原版的:

Without knowing what SQL version you are using, it's harder to answer that portion of your question. 如果不知道您使用的是哪个SQL版本,则很难回答问题的这一部分。 I think most of them support a DATEPART function, so you could try: 我认为其中大多数都支持DATEPART函数,因此您可以尝试:

ORDER BY DATEPART(dd, YourDateField) DESC

As far as JavaScript/jQuery: 至于JavaScript / jQuery:

var d = new Date('Tue Nov 20 2012 16:20:55 GMT+0000 (GMT)');

will return a date object in JavaScript which you can then sort by. 会在JavaScript中返回一个日期对象,您可以对其进行排序。 For just the date: 仅针对日期:

d.getDate(); //returns day of month

If you can be a little more specific in your question (which engine/version of SQL you are using, or how your client-side table is structured and the function you are using to sort it), then we may be able to assist you a little better. 如果您对问题(使用的是哪种SQL引擎/版本,或客户端表的结构以及用于对它进行排序的功能)更具体一些,那么我们可以为您提供帮助好一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM