以下在 SQL SERVER 中工作正常。但我无法让查询在 PhoneGap 应用程序中的 SQLite 中工作。
这是场景:
该表在每一行中都填充了许多发件人/发件人的电话号码和一条短信。我需要找出从/到电话号码组合的最后一条消息是什么。
我创建了一个如下表。(示例在 TSQL - SQL Server 中)。
Create Table SmsLog
(
messageId int identity (1,1) primary key,
messageDate datetime,
fromPhoneNumber varchar(50),
toPhoneNumber varchar(50),
textMessage varchar(50)
)
GO
然后我插入了一堆行,如下所示:
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+14255587777', 'Blah 1');
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+14255587777', 'Blah 2');
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+14255587777', 'Blah 3');
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+12149653333', 'Blah 4');
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+12149653333', 'Blah 11');
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+14255587777', 'Blah 12');
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+14254490006', 'Blah 13');
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+14254490006', 'Blah 21');
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+14254490087', 'Blah 22');
Insert into SmsLog(MessageDate, FromPhoneNumber, ToPhoneNumber, TextMessage) Values (GetDate(), '+19175380077', '+14254490087', 'Blah 23');
GO
以下查询应该可以完成工作(这在 MSSQL 中非常有效)。
/* Retrieve the last message per from/to from the table*/
SELECT *
FROM SmsLog
WHERE MessageId IN
(
SELECT MAX(MessageId) as MessageId
FROM SmsLog
GROUP
BY FromPhoneNumber, ToPhoneNumber
)
GO
MSSQL 的结果如下(这正是我需要使用 SQLite 作为其数据存储的应用程序):
5 2012-10-28 23:32:23.257 +19175380077 +12149653333 Blah 11
6 2012-10-28 23:32:23.257 +19175380077 +14255587777 Blah 12
8 2012-10-28 23:32:23.257 +19175380077 +14254490006 Blah 21
10 2012-10-28 23:32:23.257 +19175380077 +14254490087 Blah 23
但我无法在 SQLite for PhoneGap 应用程序(在 iOS 上)中使用相同的查询。javaScript 代码如下:
db.transaction(function(tx)
{
var sqlString = "SELECT * FROM smsLog WHERE messageId IN (SELECT MAX(messageId) AS messageId FROM smsLog GROUP BY fromPhoneNumber, toPhoneNumber";
tx.executeSql (sqlString, [],
function(tx, result)
{
if (console)
{
console.log("in retrive success");
}
var liblock = "";
$.each(result.rows,function(index)
{
var row = result.rows.item(index);
liblock +=
('<li><a href="#"><h3>'+row['toPhoneNumber'] + '</h3><p>' +row['textMessage']+'</p><p class="ui-li-aside"><strong>'+ formatDate(new Date(row['messageDate'])) + '</strong></p></a></li>');
});
$("#topicList").html(liblock);
$("#topicList").listview("refresh");
},
function(err)
{
if (console)
{
console.log(err);
}
});
},
function (dbError)
{
if (console)
{
console.log("ERROR Performing database operation");
console.log(dbError);
}
},
function (dbSuccess)
{
if (console)
{
console.log("Record retreived");
}
});
}