创建一个视图,其中来自同一列的2条记录位于同一行

时间:2018-08-22 11:29:24

标签: mysql

(首先我是新来的,所以如果问一个问题我可以做得更好/不同,请通知我)

我在MySQL数据库中有一个表,如下所示:

ID  Time    Object
1   10:00:05    1021
2   10:00:10    1021
3   10:00:15    1021
4   10:00:20    1021
5   10:00:25    1021
6   10:00:30    1021
7   10:00:35    1022
8   10:00:40    1022
9   10:00:45    1022
10  10:00:50    2300
11  10:00:55    2300
12  10:01:00    2300
14  10:01:10    2300 
15  10:01:15    2401
16  10:01:20    2503
18  10:01:30    2503

之所以要在视图中而不是在应用程序中执行此操作,是因为我想减少数据进入数据库之前所需的步骤。

现在,我希望在视图中具有以下格式。 从原始表中的所有记录中,我想要在同一行之后有该记录。之后的记录并不总是具有比第一个高1的ID,但是它总是更高。第一个记录将标有“ _1”,之后的记录将标有“ _2”。我想对原始表中的所有3列执行此操作。

ID_1    ID_2    Time_1  Time_2  Object_1    Object_2
1        2     10:00:05 10:00:10    1021    1021
2        3     10:00:10 10:00:15    1021    1021
3        4     10:00:15 10:00:20    1021    1021
4        5     10:00:20 10:00:25    1021    1021
5        6     10:00:25 10:00:30    1021    1021
6        7     10:00:30 10:00:35    1021    1022
7        8     10:00:35 10:00:40    1022    1022
etc...

当我开始创建视图时,我陷入困境,无法创建“ 2”列。.我尝试在线搜索类似的问题,但找不到对我有用的任何东西。

这是我唯一拥有的东西:

VIEW `View` AS
    SELECT 
        `Table`.`ID` AS `ID-1`,
        `Table`.`Time` AS `Time-1`,
        `Table`.`Object` AS `Object-1`

    FROM
        `Table`
    ORDER BY `Table`.`ID`  

我希望有人能够帮助我/将我推向正确的方向:)

最好的问候, 梅杰丹

4 个答案:

答案 0 :(得分:0)

您似乎在将奇数编号连接到下一个编号,因此自我连接可能与mod(%)测试有关

select t.id,t1.id,t.time,t1.time,t.object,t1.object
from t
left join t t1 on t1.id = t.id + 1 and t.object = t1.object
where t.id % 2 > 0
order by t.object,t.id;

+------+------+----------+----------+--------+--------+
| id   | id   | time     | time     | object | object |
+------+------+----------+----------+--------+--------+
|    1 |    2 | 10:00:05 | 10:00:10 |   1021 |   1021 |
|    3 |    4 | 10:00:15 | 10:00:20 |   1021 |   1021 |
|    5 |    6 | 10:00:25 | 10:00:30 |   1021 |   1021 |
|    7 |    8 | 10:00:35 | 10:00:40 |   1022 |   1022 |
|    9 | NULL | 10:00:45 | NULL     |   1022 |   NULL |
|   11 |   12 | 10:00:55 | 10:01:00 |   2300 |   2300 |
|   13 |   14 | 10:01:05 | 10:01:10 |   2300 |   2300 |
|   15 | NULL | 10:01:15 | NULL     |   2401 |   NULL |
|   17 |   18 | 10:01:25 | 10:01:30 |   2503 |   2503 |
+------+------+----------+----------+--------+--------+
9 rows in set (0.00 sec)

如果您不相信ID是连续的(并且您可能不应该这样做),那么如果您使用的是mysql版本8或更好,请使用lag函数(如果模拟用户行数低于8,则更好)。

通过行号模拟,代码如下所示

select tid,t1id,ttime,t1time,tobject,t1object
from
(
select t.id tid,t.time ttime, t.object tobject, 
        if(t.object<> @p,@rn:=1,@rn:=@rn+1) rn,
        @p:=t.object p
from t
cross join (select @rn:=0,@p:=0) r
order by object , time
) t
left join
(
select t.id t1id, t.time t1time, t.object t1object,
        if(t.object<> @p1,@rn1:=1,@rn1:=@rn1+1) rn,
        @p1:=t.object p
from t
cross join (select @rn1:=0,@p1:=0) r
order by object , time
) t1
on t1object = tobject and t1.rn = t.rn + 1
where t.rn % 2 > 0
order by tobject,tid;

更详细,但也可能更健壮。

答案 1 :(得分:0)

您需要将表连接到自身,并为每个表赋予单独的名称 然后从每个表中选择所需的列。 SELECT t1.`ID` AS `ID-1`, t2.`ID` AS `ID-2`, . . . FROM `posrep` as t1 join `posrep` as t2 WHERE <how you plan to decide on each value>

答案 2 :(得分:0)

尝试此操作,请用您的表名替换test

            SELECT  a.id as ID_1,
                b.id as ID_2,
                a.Time as Time_1,
                b.Time as Time_2,
                a.Object as Object_1,
                b.Object as Object_2
            FROM 
            (SELECT * FROM `test`) as a,
            (SELECT * FROM `test`) as b 
            WHERE a.id+1 = b.id

答案 3 :(得分:0)

使用以下SQL查询创建视图,并将“ table_name”替换为实际的表名。

从“ table_name”中选择ID作为ID_1,ID + 1作为ID_2;