我们如何链接MySQL中的行?

时间:2019-07-16 06:59:08

标签: mysql

我要链接单列数据,以使其删除连续的重复数据并将其链接到下一行。以下是数据示例。

|  id  |  order_id |  issue_id  
------------------------------- 
|  1   |  7735436  |  795   
|  2   |  7735436  |  795   
|  3   |  7735436  |  94   
|  4   |  7735436  |  94   
|  5   |  7735436  |  795   
|  6   |  7735436  |  795   
|  7   |  7735436  |  42   
|  8   |  7735436  |  42   
|  9   |  7735436  |  795   
|  10  |  7735436  |  758   
|  11  |  7735436  |  758   
|  14  |  935437   |  23   
|  15  |  935437   |  23   
|  16  |  935437   |  91   
|  17  |  935437   |  92   
|  18  |  935437   |  92   
|  19  |  935437   |  105   
|  20  |  935437   |  105   
|  21  |  935437   |  105   
|  22  |  935437   |  82   
|  23  |  935437   |  82   
|  24  |  935437   |  10   

预期输出为

|  order_id | link_1 | link_2   
----------------------------
|  7735436  | 795    | 94   
|  7735436  | 94     | 795   
|  7735436  | 795    | 42  
|  7735436  | 42     | 795  
|  7735436  | 795    | 758   
|  935437   | 23     | 91   
|  935437   | 91     | 92  
|  935437   | 92     | 105 
|  935437   | 105    | 82   
|  935437   | 82     | 10    

1 个答案:

答案 0 :(得分:2)

这是一个解析(窗口)函数问题,需要使用Lead()。但是,这些好东西仅在MySQL 8.0.2+中可用;因此,对于您的情况(MySQL 5.7),一种解决方案是利用Correlated Subqueries

SELECT 
  DISTINCT t1.order_id, 
           t1.issue_id AS link_1, 
           (SELECT t2.issue_id 
            FROM your_table_name AS t2 
            WHERE t2.id > t1.id 
                  AND t2.order_id = t1.order_id 
                  AND t2.issue_id <> t1.issue_id 
            ORDER BY t2.id ASC LIMIT 1) AS link_2 
FROM your_table_name AS t1

结果:View on DB Fiddle

| order_id | link_1 | link_2 |
| -------- | ------ | ------ |
| 7735436  | 795    | 94     |
| 7735436  | 94     | 795    |
| 7735436  | 795    | 42     |
| 7735436  | 42     | 795    |
| 7735436  | 795    | 758    |
| 7735436  | 758    |        |
| 935437   | 23     | 91     |
| 935437   | 91     | 92     |
| 935437   | 92     | 105    |
| 935437   | 105    | 82     |
| 935437   | 82     | 10     |
| 935437   | 10     |        |

现在,在上述方法中,您将获得两个额外的行,对应于没有link_2issue_id没有后续的order_id)的情况。您可以在应用程序代码中忽略它们;或者让您的查询在WHERE子句中进一步处理;或使用子查询(Derived Table)。我宁愿在应用程序代码中处理它,而不是进一步加重查询负担。


但是,如果您想处理SQL查询本身中的所有内容,那么就可以了:

SELECT 
  DISTINCT t1.order_id, 
           t1.issue_id AS link_1, 
           (SELECT t2.issue_id 
            FROM your_table_name AS t2 
            WHERE t2.id > t1.id 
                  AND t2.order_id = t1.order_id 
                  AND t2.issue_id <> t1.issue_id 
            ORDER BY t2.id ASC LIMIT 1) AS link_2 
FROM your_table_name AS t1 
WHERE 
(SELECT t2.issue_id 
            FROM your_table_name AS t2 
            WHERE t2.id > t1.id 
                  AND t2.order_id = t1.order_id 
                  AND t2.issue_id <> t1.issue_id 
            ORDER BY t2.id ASC LIMIT 1) IS NOT NULL

结果#2:View on DB Fiddle

| order_id | link_1 | link_2 |
| -------- | ------ | ------ |
| 7735436  | 795    | 94     |
| 7735436  | 94     | 795    |
| 7735436  | 795    | 42     |
| 7735436  | 42     | 795    |
| 7735436  | 795    | 758    |
| 935437   | 23     | 91     |
| 935437   | 91     | 92     |
| 935437   | 92     | 105    |
| 935437   | 105    | 82     |
| 935437   | 82     | 10     |