按顺序连接表

时间:2017-01-18 21:37:55

标签: mysql sql

让我说,我有两个表PTO_CONTROL和RUTA_PTO, 我尝试只用它们的FK_PTO加入这个表 按提供PTO_CONTROL表的顺序共享。 直到现在的问题是始终获得第一个 注册在搜索中找到,获取重复值。 我如何创建sql指令来解决这个问题?感谢。

TABLE PTO_CONTROL
ID  FK_PTO  NUM     HORA
55  122     566     5:02        
56  104     568     5:16
57  114     572     5:38
58  104     573     6:12
59  110     577     6:28
60  122     582     6:45

TABLE RUTA_PTO
ID  FK_RUTA FK_PTO  TIEMPO
15  56      122     0
16  56      104     12
17  56      114     22
18  56      104     32
19  56      121     40
20  56      110     45
21  56      122     55

JOIN TABLE / VALUES EXPECTED
FK_PTO  NUM HORA TIEMPO
122     566 5:02 0
104     568 5:16 12
114     572 5:38 22
104     573 6:12 32
110     577 6:28 45
122     582 6:45 55

DML和sql指令可以是,


    CREATE TABLE pto (
     ID INT PRIMARY KEY,
     NOMBRE VARCHAR(32)
    );

    CREATE TABLE ruta (
     ID INT PRIMARY KEY,
     NOMBRE VARCHAR(32)
    );

    CREATE TABLE ruta_pto (
     ID INT PRIMARY KEY,
     FK_RUTA INT NOT NULL,
     FK_PTO INT NOT NULL,
     TIEMPO INT,
     CONSTRAINT FK_RUTA_PTO_PTO
      FOREIGN KEY (FK_PTO)
      REFERENCES pto (ID)
     CONSTRAINT FK_RUTA_PTO_RUTA
      FOREIGN KEY (FK_RUTA)
      REFERENCES ruta (ID)
    );

    CREATE TABLE pto_control (
     ID INT PRIMARY KEY,
     FK_PTO INT NOT NULL,
     NUM INT,
     HORA TIME,
     CONSTRAINT FK_PTO_CONTROL_PTO
      FOREIGN KEY (FK_PTO)
      REFERENCES pto (ID)
    );

    SELECT
     pc.FK_PTO,
     pc.NUM,
     pc.HORA,
     rp.TIEMPO
    FROM pto_control AS pc
    INNER JOIN ruta_pto AS rp ON
     pc.FK_PTO = rp.FK_PTO AND rp.FK_RUTA = 56
    INNER JOIN pto AS p ON
     pc.FK_PTO = p.ID;

    and the wrong result that obtain is

    FK_PTO  NUM HORA TIEMPO
    122     566 5:02 0
    104     568 5:16 12
    114     572 5:38 22
    104     568 5:16 12
    110     577 6:28 45
    122     566 5:02 0

1 个答案:

答案 0 :(得分:0)

您需要为两个表实现组排名。这可以通过以下查询完成:

select c1.*, count(*) as rank
from PTO_CONTROL c1
join PTO_CONTROL c2
  on  c2.FK_PTO = c1.FK_PTO
  and c2.ID <= c1.ID
group by c1.ID;

结果:

| ID | FK_PTO | NUM | HORA | rank |
|----|--------|-----|------|------|
| 55 |    122 | 566 | 5:02 |    1 |
| 56 |    104 | 568 | 5:16 |    1 |
| 57 |    114 | 572 | 5:38 |    1 |
| 58 |    104 | 573 | 6:12 |    2 |
| 59 |    110 | 577 | 6:28 |    1 |
| 60 |    122 | 582 | 6:45 |    2 |

select r1.*, count(*) as rank
from RUTA r1
join RUTA r2
  on  r2.FK_PTO = r1.FK_PTO
  and r2.ID <= r1.ID
group by r1.ID;

结果:

| ID | FK_RUTA | FK_PTO | TIEMPO | rank |
|----|---------|--------|--------|------|
| 15 |      56 |    122 |      0 |    1 |
| 16 |      56 |    104 |     12 |    1 |
| 17 |      56 |    114 |     22 |    1 |
| 18 |      56 |    104 |     32 |    2 |
| 19 |      56 |    121 |     40 |    1 |
| 20 |      56 |    110 |     45 |    1 |
| 21 |      56 |    122 |     55 |    2 |

现在,您可以使用生成的rank

加入两个结果
select *
from (
  select c1.*, count(*) as rank
  from PTO_CONTROL c1
  join PTO_CONTROL c2
    on  c2.FK_PTO = c1.FK_PTO
    and c2.ID <= c1.ID
  group by c1.ID
) c
join (
  select r1.*, count(*) as rank
  from RUTA r1
  join RUTA r2
    on  r2.FK_PTO = r1.FK_PTO
    and r2.ID <= r1.ID
  group by r1.ID
) r using (FK_PTO, rank)

结果:

| FK_PTO | NUM | HORA | TIEMPO |
|--------|-----|------|--------|
|    122 | 566 | 5:02 |      0 |
|    104 | 568 | 5:16 |     12 |
|    114 | 572 | 5:38 |     22 |
|    104 | 573 | 6:12 |     32 |
|    110 | 577 | 6:28 |     45 |
|    122 | 582 | 6:45 |     55 |

http://sqlfiddle.com/#!9/f3f427/5