MySQL-链接表问题(选择唯一行)

时间:2019-04-15 14:22:02

标签: mysql join row-number

我一直在尝试解决问题,但失败了。我的问题下面有快照,该问题已被删除。我想要的是以下内容:

action_id   event_id    link    text    duration
339054      10221877    317547  text1   30
339057      10221877    317548  text2   45
339061      10221877    317549  text3   30
339065      10221877    317551  text4   40
339074      10221877    317550  text5   60
339075      10221877    317552  text6   80

仅使用两个表是否有可能?如果没有,那会有什么帮助?

CREATE TABLE time1
(

prim int (5),
booking int (5) PRIMARY KEY,
duration int (5));

insert into time1 values 
( 10221877, 296480,30),
( 10221877, 296484,45),
( 10221877, 296487,30),
( 10221877, 296492,40),
( 10221877, 296494,60),
( 10221877, 296510,80),
( 10221877, 296511,11);


CREATE TABLE action1
(

action_id int (5) PRIMARY KEY,
event_id int (5),
link int (5),
text_text VARCHAR(255)

);

insert into action1 values 
(339054,10221877,317547,"text1"),
(339057,10221877,317548,"text2"),
(339061,10221877,317549,"text3"),
(339065,10221877,317551,"text4"),
(339074,10221877,317550,"text5"),
(339075,10221877,317552,"text6");

4 个答案:

答案 0 :(得分:2)

正如我所看到的,您想要将action1表的第一行连接到time1表的第一行,将action1表的第二行连接到time1的第二行,依此类推。因此,您可以使用variables计算有序的行数,并据此连接表。例如:

SELECT a.action_id, a.event_id, a.link, a.text_text, t.duration
FROM (SELECT action_id,
             event_id,
             link,
             text_text,
             IF(@event_id = event_id, @rownumber := @rownumber + 1, @rownumber := 0) as rownumber,
             @event_id := event_id
      FROM action1,
           (SELECT @event_id := 0, @rownumber := 0) AS rn) as a
         LEFT JOIN
     (SELECT duration,
             prim,
             IF(@prim = prim, @rownumber := @rownumber + 1, @rownumber := 0) as rownumber,
             @prim := prim
      FROM time1,
           (SELECT @prim := 0, @rownumber := 0) AS rn) as t ON a.event_id = t.prim AND a.rownumber = t.rownumber

结果是

--------------------------------------------------------
| action_id | event_id | link   | text_text | duration |
--------------------------------------------------------
|    339054 | 10221877 | 317547 | text1     |       30 |
|    339057 | 10221877 | 317548 | text2     |       45 |
|    339061 | 10221877 | 317549 | text3     |       30 |
|    339065 | 10221877 | 317551 | text4     |       40 |
|    339074 | 10221877 | 317550 | text5     |       60 |
|    339075 | 10221877 | 317552 | text6     |       80 |
--------------------------------------------------------

DBFiddle中的DEMO

答案 1 :(得分:1)

您想要的内容看起来像JOIN,请看一下this answer right here,因为它有很多有关联接的信息,我将尝试执行以下查询

SELECT * FROM `time1` INNER JOIN `action1` ON `time1`.`prim` = `action1`.`event_id`

如果我理解您正确输入的数据。

答案 2 :(得分:0)

您可以使用windows functions尝试以下类似操作以获得结果。在这里,我将加入2个表并为每行分配row_number(),然后使用DENSE_RANK()分配组号,然后通过匹配行号和组号来获得结果。希望这会有所帮助。

select action_id,event_id,link,text_text,duration from (
    select action1.*,duration,(row_number() over (partition by action1.action_id)) as rowno,
           (dense_rank() over (order by action1.action_id)) as groupno  from action1
    join time1 
    on action1.event_id = time1.prim
) as mytable
where mytable.rowno = mytable.groupno

您可以在此处查看结果:DEMO HERE

答案 3 :(得分:0)

我在MySQL 8.0上使用窗口函数来完成此工作:

SELECT a.action_id, a.event_id, a.link, a.text_text, t.duration
FROM (SELECT *, ROW_NUMBER() OVER () AS rownum FROM time1 ORDER BY booking) AS t
JOIN (SELECT *, ROW_NUMBER() OVER () AS rownum FROM action1 ORDER BY action_id) AS a
  USING (rownum)

输出:

+-----------+----------+--------+-----------+----------+
| action_id | event_id | link   | text_text | duration |
+-----------+----------+--------+-----------+----------+
|    339054 | 10221877 | 317547 | text1     |       30 |
|    339057 | 10221877 | 317548 | text2     |       45 |
|    339061 | 10221877 | 317549 | text3     |       30 |
|    339065 | 10221877 | 317551 | text4     |       40 |
|    339074 | 10221877 | 317550 | text5     |       60 |
|    339075 | 10221877 | 317552 | text6     |       80 |
+-----------+----------+--------+-----------+----------+

我不得不评论,根据行号加入行是非常规的。行不应该有任何隐式顺序,您应该仅按值而不是按行位置连接它们。

在您的示例中,booking的数字似乎正在增加,但是我不知道这是您数据中的可靠假设还是示例中的巧合。而且我不知道我是否做出正确的假设,即您希望按位置将行连接在一起。符合您的示例,但再次可能只是巧合。

从您的示例中也无法得知当存在多个event_id值时如何进行联接。您是要按row_number()重新开始行编号,还是只是逐行匹配?如果两个表中每个event_id的行数不同,该怎么办?您需要使用LEFT OUTER JOIN吗?还是FULL OUTER JOIN