MYSQL SQL(自)连接?
使用此示例数据集:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
接近:
gets close:
在 Marc 的基础上进行操作:
这对于更复杂的查询可能会有所帮助:
然后使用它:
Operating on Marc's basis:
This might be helpful with more complex queries:
and then use it:
这应该准确地返回您在第一组所需输出中所拥有的内容。
This should return exactly what you have in your first set of desired output.