MYSQL SQL(自)连接?

发布于 2024-11-02 22:32:38 字数 1251 浏览 0 评论 0原文

使用此示例数据集:

CREATE TABLE test. test2 (id VARCHAR(7), AA INT, BBB INT, CCC VARCHAR (12));
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A123', 45, 123, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A120', 52, 120, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A133', 63, 133, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D123', 34, 123, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D120' ,32, 120, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D140', 12, 140, '2011-04' ); 

我寻找具有 3 列的表。

Col A“Id”顺序为原始的 DES。

B 列“Id2”作为 A 列中的上一行或下一行 Id,其中 CCC 相同。

Id,  Id2  CCC
A120 A123 '2011-03'
A123 A133 '2011-03'
A133      '2011-03'
D120 D123 '2011-04'
D123 D140 '2011-04'
D140      '2011-04'

或者

  Id,  Id2  CCC
A120      '2011-03'
A123 A120 '2011-03'
A133 A123 '2011-03'
D120      '2011-04'
D123 D120 '2011-04'
D140 D123 '2011-04'

或者

   Id,  Id2  CCC
   A123 A120 '2011-03'
   A133 A123 '2011-03'
   D123 D120 '2011-04'
   D140 D123 '2011-04'

我可以向连接表添加一个 autocrement col,然后向上或向下使用 1、2、3 行吗?那么 id2 将基于此自动增量行吗?

Using this sample dataset:

CREATE TABLE test. test2 (id VARCHAR(7), AA INT, BBB INT, CCC VARCHAR (12));
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A123', 45, 123, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A120', 52, 120, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'A133', 63, 133, '2011-03' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D123', 34, 123, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D120' ,32, 120, '2011-04' );
INSERT INTO test.test2 (id, AA, BBB,CCC) VALUES ( 'D140', 12, 140, '2011-04' ); 

Im look for table with 3 column.

Col A "Id" order as Desc from orginal.

Col B "Id2" as previous or next row Id in col A where CCC are the same.

Id,  Id2  CCC
A120 A123 '2011-03'
A123 A133 '2011-03'
A133      '2011-03'
D120 D123 '2011-04'
D123 D140 '2011-04'
D140      '2011-04'

or

  Id,  Id2  CCC
A120      '2011-03'
A123 A120 '2011-03'
A133 A123 '2011-03'
D120      '2011-04'
D123 D120 '2011-04'
D140 D123 '2011-04'

OR

   Id,  Id2  CCC
   A123 A120 '2011-03'
   A133 A123 '2011-03'
   D123 D120 '2011-04'
   D140 D123 '2011-04'

Could I add an autocrement col to joined table, then use 1, 2, 3 rows up or down? The id2 would then be based on this autocrement row?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

若无相欠,怎会相见 2024-11-09 22:32:38
SELECT test.id, child.id, test.CCC
FROM test
LEFT JOIN test AS child ON ((test.CCC = child.CCC) and (test.id < child.id))

接近:

+------+------+---------+
| id   | id   | CCC     |
+------+------+---------+
| A120 | A123 | 2011-03 |
| A120 | A133 | 2011-03 |
| A123 | A133 | 2011-03 |
| A133 | NULL | 2011-03 |
| D120 | D123 | 2011-04 |
| D120 | D140 | 2011-04 |
| D123 | D140 | 2011-04 |
| D140 | NULL | 2011-04 |
+------+------+---------+
SELECT test.id, child.id, test.CCC
FROM test
LEFT JOIN test AS child ON ((test.CCC = child.CCC) and (test.id < child.id))

gets close:

+------+------+---------+
| id   | id   | CCC     |
+------+------+---------+
| A120 | A123 | 2011-03 |
| A120 | A133 | 2011-03 |
| A123 | A133 | 2011-03 |
| A133 | NULL | 2011-03 |
| D120 | D123 | 2011-04 |
| D120 | D140 | 2011-04 |
| D123 | D140 | 2011-04 |
| D140 | NULL | 2011-04 |
+------+------+---------+
一腔孤↑勇 2024-11-09 22:32:38

在 Marc 的基础上进行操作:

SELECT test.id, child.id, test.CCC
FROM test
  LEFT JOIN test AS child
    ON (test.CCC = child.CCC)
      AND (test.id < child.id)
WHERE NOT EXISTS
  ( SELECT 1
    FROM test AS middle
    WHERE (test.CCC = middle.CCC)
      AND (test.id < middle.id)
      AND (middle.id < child.id)
  )
  OR child.id IS NULL
ORDER BY test.id

这对于更复杂的查询可能会有所帮助:

CREATE VIEW testWithRowId AS
  ( SELECT test.id
         , COUNT(test.id) AS rownum
         , test.CCC
    FROM test
      JOIN test AS child
        ON (test.CCC = child.CCC)
          AND (test.id >= child.id)
    GROUP BY test.CCC
           , test.id 
  )

然后使用它:

SELECT t1.id
     , t2.id AS idShifted
     , t1.CCC
FROM testWithRowId t1
  LEFT JOIN testWithRowId t2
    ON (t2.CCC = t1.CCC) 
      AND (t2.rownum = t1.rownum + 1)   ---- replace this 1 with 2 or 3, etc
ORDER BY t1.CCC                         ---- for a shift 2 or shift 3, etc
       , t1.rownum

Operating on Marc's basis:

SELECT test.id, child.id, test.CCC
FROM test
  LEFT JOIN test AS child
    ON (test.CCC = child.CCC)
      AND (test.id < child.id)
WHERE NOT EXISTS
  ( SELECT 1
    FROM test AS middle
    WHERE (test.CCC = middle.CCC)
      AND (test.id < middle.id)
      AND (middle.id < child.id)
  )
  OR child.id IS NULL
ORDER BY test.id

This might be helpful with more complex queries:

CREATE VIEW testWithRowId AS
  ( SELECT test.id
         , COUNT(test.id) AS rownum
         , test.CCC
    FROM test
      JOIN test AS child
        ON (test.CCC = child.CCC)
          AND (test.id >= child.id)
    GROUP BY test.CCC
           , test.id 
  )

and then use it:

SELECT t1.id
     , t2.id AS idShifted
     , t1.CCC
FROM testWithRowId t1
  LEFT JOIN testWithRowId t2
    ON (t2.CCC = t1.CCC) 
      AND (t2.rownum = t1.rownum + 1)   ---- replace this 1 with 2 or 3, etc
ORDER BY t1.CCC                         ---- for a shift 2 or shift 3, etc
       , t1.rownum
季末如歌 2024-11-09 22:32:38
Select T.Id, NextTest.NextId As Id2, T.CCC
From Test2 As T
    Left Join   (
                Select T1.Id, Min( T2.Id ) As NextId
                From Test2 As T1
                    Left Join Test2 As T2
                        On T2.CCC = T1.CCC
                            And T2.Id > T1.Id
                Group By T1.Id
                ) As NextTest
            On NextTest.Id = T.Id
Order By T.Id       

这应该准确地返回您在第一组所需输出中所拥有的内容。

Select T.Id, NextTest.NextId As Id2, T.CCC
From Test2 As T
    Left Join   (
                Select T1.Id, Min( T2.Id ) As NextId
                From Test2 As T1
                    Left Join Test2 As T2
                        On T2.CCC = T1.CCC
                            And T2.Id > T1.Id
                Group By T1.Id
                ) As NextTest
            On NextTest.Id = T.Id
Order By T.Id       

This should return exactly what you have in your first set of desired output.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文