如何将当前数据与SqlDataReader中的下一个数据进行比较

时间:2017-06-09 09:43:30

标签: c# mysql

我有这张桌子:

 ID
00001
00001
00002
00002
00003
00004
00004
00004
00005

如果SqlDataReader到达第5列(即00003,我是否有可能比较下一列是否具有相同{{1}在完成当前ID循环之前的当前值?

我将使用它来确定读者是否会移动到下一个不同的while,以便我可以绘制最后一部分的控件,然后继续下一个ID。< / p>

我很想发布代码,但由于我是动态创建元素,所以它很长。

修改

以下是代码(为了使其尽可能简化而清理)

ID

使用此代码,我设法在创建了类似bool isFinished = false; string lastID = ""; while (reader.Read()) { string transID = Convert.ToInt32(reader["ID"]).ToString("D5"); lastID = (lastID == "") ? transID : lastID; isFinished = (lastID != transID) ? true : false; if (isFinished) { LastPart(); } initialParts(); lastID = transID; } 的所有数据之后放置LastPart()。但这导致ID在最后LastPart()

之后未被调用

1 个答案:

答案 0 :(得分:0)

这是SQL中的一个示例。在 union ALL 部分,您可以填写查询:

SELECT *
FROM
  (SELECT @my_id:=@my_last_id AS new_id , @my_last_id:=id AS next_id
   FROM
     ( SELECT *
      FROM
        ( SELECT *
         FROM
           ( SELECT "00001" AS id
            UNION ALL SELECT "00001"
            UNION ALL SELECT "00002"
            UNION ALL SELECT "00002"
            UNION ALL SELECT "00003"
            UNION ALL SELECT "00004"
            UNION ALL SELECT "00004"
            UNION ALL SELECT "00004"
            UNION ALL SELECT "00005" ) AS yourTable
         UNION ALL SELECT NULL AS id ) tablePlusOneRow) newTable
   CROSS JOIN
     (SELECT @my_id:=NULL, @my_next_id:=NULL) init) AS new_table2
WHERE new_id IS NOT NULL;

<强>样品

MariaDB [sample]> SELECT *
    -> FROM
    ->   (SELECT @my_id:=@my_last_id AS new_id , @my_last_id:=id AS next_id
    ->    FROM
    ->      ( SELECT *
    ->       FROM
    ->         ( SELECT *
    ->          FROM
    ->            ( SELECT "00001" AS id
    ->             UNION ALL SELECT "00001"
    ->             UNION ALL SELECT "00002"
    ->             UNION ALL SELECT "00002"
    ->             UNION ALL SELECT "00003"
    ->             UNION ALL SELECT "00004"
    ->             UNION ALL SELECT "00004"
    ->             UNION ALL SELECT "00004"
    ->             UNION ALL SELECT "00005" ) AS yourTable
    ->          UNION ALL SELECT NULL AS id ) tablePlusOneRow) newTable
    ->    CROSS JOIN
    ->      (SELECT @my_id:=NULL, @my_next_id:=NULL) init) AS new_table2
    -> WHERE new_id IS NOT NULL;
+--------+---------+
| new_id | next_id |
+--------+---------+
| 00001  | 00001   |
| 00001  | 00002   |
| 00002  | 00002   |
| 00002  | 00003   |
| 00003  | 00004   |
| 00004  | 00004   |
| 00004  | 00004   |
| 00004  | 00005   |
| 00005  | NULL    |
+--------+---------+
9 rows in set (0.00 sec)

MariaDB [sample]>

速度1000000 ROWS

时间也包括输出时间0.28 / 0.79秒

MariaDB [sample]> select * from myids;

.....
|  999998 |
|  999999 |
| 1000000 |
+---------+
1000000 rows in set (0.28 sec)

MariaDB [sample]>

    SELECT * FROM (
    SELECT @my_id:=@my_last_id AS new_id , @my_last_id:=id AS next_id
    FROM (
        SELECT * 
        FROM (
        SELECT * 
        FROM (
            SELECT * FROM myids
        ) AS yourTable
        UNION ALL 
        SELECT NULL AS id
        ) tablePlusOneRow
    ) newTable
    CROSS JOIN ( SELECT @my_id:=NULL, @my_next_id:=NULL) init
    ) AS new_table2
    WHERE new_id IS NOT NULL;

    ....
    |  999998 |  999999 |
    |  999999 | 1000000 |
    | 1000000 |    NULL |
    +---------+---------+
    1000000 rows in set (0.79 sec)