【问题标题】:Sorting data from two different sorted cursors data of different tables into One将来自不同表的两个不同排序游标数据的数据排序为一个
【发布时间】:2013-09-04 11:53:14
【问题描述】:

我想对来自两个游标的结果进行排序。

让我们考虑两个游标是cursor1cursor2

Cursor1 用于table XCursor2 用于table Y

Cursor1 使用 date 类型的列 Start 对数据进行排序,Cursor2 使用 date 类型的列 Date 对数据进行排序。

有些字段在两个表中是通用的,有些则不是。

但是,两个表之间没有绝对关系。

问题很明显。合并后的数据应该是两个游标的排序列表。

现在发生的事情是:

我从光标Cursor1 获得排序列表,它独立于来自光标Cursor2 的排序列表。

如何合并生成的游标数据以便按两个日期 (Start and Date) 获取排序列表?

例如:

我得到了这个结果:

| Date                 | Type        | Location              |
|:---------------------|------------:|:---------------------:|
| 10-Jul-2013 07:05:00 | Random      | Washougal, Washington
| 10-Jul-2013 08:30:00 | Single      | Vancouver, Washington
| 10-Jul-2013 07:30:00 | Multiple    | Vancouver, Washington
| 10-Jul-2013 15:31:00 | Double      | Vancouver, Washington

前两行来自表 X,最后两行来自表 Y。

但我想要这个结果:

| Date                 | Type        | Location              |
|:---------------------|------------:|:---------------------:|
| 10-Jul-2013 07:05:00 | Random      | Washougal, Washington
| 10-Jul-2013 07:30:00 | Multiple    | Vancouver, Washington
| 10-Jul-2013 08:30:00 | Single      | Vancouver, Washington
| 10-Jul-2013 15:31:00 | Double      | Vancouver, Washington

查询应该是:

Cursor1 = Select alpha, beeta, gamma, Remark, id, number from X order by Start ASC


Cursor2 = Select Type, Date, gamma, Location, Obs, number from Y order by Date ASC

从 Cursor1 得到结果后,我在这样的循环中创建字符串 html:

String itemList = "";

itemList += "<tr><td align='left' width='28%' style='font-size:8px;padding-left: 15px'>"
    + cursor1.getString(1)
    + "</td><td width='16%' style='font-size:8px'>"
    + cursor1.getString(0)
    + "</td><td width='10%' style='font-size:8px'>"
    + cursor1.getString(2)
    + "</td><td width='20%' style='font-size:8px'>" 
    + "</td><td width='35%'>" + cursor1.getString(3) + "</td></tr>";

然后将此 itemList 进一步添加到来自 Cursor2 的结果中,以完成两个游标的 itemList。

最终的 itemList 在布局中显示为 html

【问题讨论】:

  • 可以连接两个表,但是如果你不告诉我们两个源的结构和结果应该是什么样子,没有人可以帮助你。

标签: android database sqlite sorting cursor


【解决方案1】:

您可以将两个查询合并为一个查询。

首先,确保两个结果的列数相同。 如果没有,您可能需要在一个查询中添加一些虚拟列。

然后将两者与UNION ALL结合起来:

SELECT alpha, beeta, gamma, Remark, id,   number FROM X
UNION ALL
SELECT Type,  Date,  gamma, Obs,    NULL, number FROM Y

然后从您要排序的整个结果中选择一列。 (结果的列名来自第一个查询。) 在这种情况下,Start 列不是结果的一部分,因此我们必须添加它(Date 列在第二个查询中重复,但这是它的值最终出现在结果列中的必要条件用于排序):

SELECT alpha, beeta, gamma, Remark, id,   number, Start AS SortThis FROM X
UNION ALL
SELECT Type,  Date,  gamma, Obs,    NULL, number, Date              FROM Y
ORDER BY SortThis

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    相关资源
    最近更新 更多