MySQL交叉矩阵

时间:2018-07-11 16:21:57

标签: mysql

我有一个包含三列“开始”,“结束”和“结果”的表。 我表中的数据就像

Start   End                      Result
1        2                       1,2
1        3                       1,3
1        4                       1,4
1        5                       1,5
1        N  (n means 6,7,…etc)   1,n
2        3                       2,3
2        4                       2,4
2        5                       2,5
2        6                       2,6
2        N  (n means,7,…etc)     2,n
3        4                       3,4
3        5                       3,5
3        6                       3,6
3        N  (n means 7,…etc)     3,n
4        5                       4,5
4        6                       4,6
4        N  (n means 6,7,…etc)   4,n
5        6                       5,6
5        N (n means 7,…etc)      5,n
.................................
N (n means 6,7,…etc)    N (n means 7,…etc)  N,N

现在我给出两个值,例如1,6

然后我想得到(1,2),(1,3),(1,4),(1,5),(1,6),(2,3),(2,4), (2,5),(2,6),(3,4),(3,5),(3,6),(4,5),(4,6),(5,6)

现在我给出两个值,例如2,3

然后我想得到(1,3),(1,4),(1,5),(1,6),(2,3),(2,4),(2,5), (2,6)

现在我给两个值5,6

那我想得到(1,6),(2,6),(3,6),(4,6),(5,6)

还有Vise Versa。

在这里,我们如何为该问题创建MySQL查询。

1 个答案:

答案 0 :(得分:0)

Select Result from my_table where End > 1 AND End <= 6

Select Result from my_table where End > 2 AND END <= 3

Select Result from my_table where End > 5 AND END <= 6

以下是一个示例,例如您上面已经提到的数据: https://sqliteonline.com/#fiddle-5b470f33ec077u5jjia9av4 您应该能够看到查询结果。