1

I have a SQL table with the following structure:

id Integer, (represents a userId)
version Integer,
attribute varchar(50)

So some sample rows are:

4, 1, 'test1'
4, 2, 'test2'
4, 3, 'test3'

I need to generate output in the following format:

4, 1, 'test1', 'test1'
4, 2, 'test2', 'test1'
4, 3, 'test3', 'test2'

So the format of my output would be:

id Integer,
version Integer,
attribute_current varchar,
attribute_old varchar

I already tried the following:

select versionTest.id, versionTest.version, versionTest.attribute, maxVersionTest.attribute
from versionTest
inner join
versionTest maxVersionTest
ON
versionTest.id = versionTest.id
AND
versionTest.version = 
(Select max(version) currentMaxVersion 
from versionTest maxRow
where maxRow.id = id);

The above query executes, but returns incorrect results. It only returns the largest version instead of returning rows for all versions.

Any ideas about how I should fix my query to produce the correct results? Thanks!

Note - The version numbers are not guaranteed to be sequential starting at 1. My actual database has some unusual version numbers (i.e. user 7 has version 3 and 15 with no versions 4, 5, 6, etc...).

4

5 回答 5

1

既然你提到了:

不保证版本号从 1 开始是连续的。我的实际数据库有一些不寻常的版本号(即用户 7 有版本 3 和 15,没有版本 4、5、6 等......)

MySQL 不像任何其他 RDBMS 那样支持窗口函数,您仍然可以模拟如何创建序列号并用作链接列来获取前一行。前任,

SELECT  a.ID, a.Version, a.attribute attribute_new,
        COALESCE(b.attribute, a.attribute) attribute_old
FROM
        (
            SELECT  ID, version, attribute,
                    @r1 := @r1 + 1 rn
            FROM    TableName, (SELECT @r1 := 0) b
            WHERE   ID = 4
            ORDER   BY version
        ) a
        LEFT JOIN
        (
            SELECT  ID, version, attribute,
                    @r2 := @r2 + 1 rn
            FROM    TableName, (SELECT @r2 := 0) b
            WHERE   ID = 4
            ORDER   BY version
        ) b ON a.rn = b.rn + 1
于 2013-04-24T13:17:20.010 回答
1
SELECT a.*, COALESCE(b.attribute,a.attribute) attribute_old
  FROM
     ( SELECT x.*
            , COUNT(*) rank 
         FROM versiontest x 
         JOIN versiontest y 
           ON y.id = x.id 
          AND y.version <= x.version 
        GROUP 
           BY x.id
            , x.version
     ) a
  LEFT
  JOIN
     ( SELECT x.*
            , COUNT(*) rank 
         FROM versiontest x 
         JOIN versiontest y 
           ON y.id = x.id 
          AND y.version <= x.version 
        GROUP 
           BY x.id
            , x.version
     ) b
    ON b.id = a.id 
   AND b.rank = a.rank-1;

样本输出(DEMO):

+----+---------+-----------+------+---------------+
| id | version | attribute | rank | attribute_old |
+----+---------+-----------+------+---------------+
|  4 |       1 | test1     |    1 | test1         |
|  4 |       5 | test2     |    2 | test1         |
|  4 |       7 | test3     |    3 | test2         |
|  5 |       2 | test3     |    1 | test3         |
|  5 |       3 | test4     |    2 | test3         |
|  5 |       8 | test5     |    3 | test4         |
+----+---------+-----------+------+---------------+
于 2013-04-24T13:17:58.823 回答
0

你可以试试...

SELECT ID,
       VERSION,
       ATTRIBUTE,
       (SELECT ATTRIBUTE
            FROM VERSIONTEST V3
            WHERE V3.ID = V1.ID AND
                  V3.VERSION = (SELECT MAX(VERSION)
                                    FROM VERSIONTEST V2
                                    WHERE V2.ID = V1.ID AND
                                          V2.VERSION < V1.VERSION)) AS PREVIOUSATTRIBUTE
    FROM VERSIONTEST V1;

只要版本值按数字顺序排列。

于 2013-04-24T13:22:09.747 回答
0

如果版本号总是增加1,您可以:

select  cur.id
,       cur.version
,       cur.attribute
,       coalesce(prev.attribute, cur.attribute)
from    versionTest
left join
        versionTest prev
on      prev.id = cur.id
        and prev.version = cur.version + 1
于 2013-04-24T13:17:22.067 回答
0

我认为表达这一点的最简单方法是使用相关子查询:

select id, version, attribute as attribute_current,
       (select attribute
        from VersionTest vt2
        where vt2.id = vt.id and vt2.version < vt.version
        order by vt2.version
        limit 1
       ) as attribute_prev
from VersionTest vt

此版本将NULL作为第一行的 prev 值。如果你真的想要它重复:

select id, version, attribute as attribute_current,
       coalesce((select attribute
                 from VersionTest vt2
                 where vt2.id = vt.id and vt2.version < vt.version
                 order by vt2.version
                 limit 1
                ), vt.attribute
               ) as attribute_prev
from VersionTest vt
于 2013-04-24T14:36:49.440 回答