MySQl创建Self连接

时间:2011-04-22 21:46:50

标签: mysql sql join

使用上一个问题的答案? MYSQL SQL (self) join?

使用视图是行不通的。行数为4000乘7列。 我可以在下面的表格中找到表格。

"id"    "AA"    "BBB"   "CCC"       Rank   Span

"A123"  "45"    "123"   "2011-03"    1       2 
"A120"  "52"    "120"   "2011-03"    2       3
"A133"  "63"    "133"   "2011-03"    3       4 
"D123"  "34"    "123"   "2011-04"    4       5
"D120"  "32"    "120"   "2011-04"    5       6
"D140"  "12"    "140"   "2011-04"    6       7 

仍然使用排名和跨度列查找下面的ans 跨度只是Rank +1或+2

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

1 个答案:

答案 0 :(得分:1)

select 
  a.id
  , b.id as id2
  , a.ccc
from table1 a
inner join table1 b on (b.span = a.rank)