mysql中整数的匹配函数
我有一个桌子结构mysql中整数的匹配函数,mysql,sql,indexing,match,rank,Mysql,Sql,Indexing,Match,Rank,我有一个桌子结构 ID Col_1 col_2 col_3 col_4 max 1 34 23 45 32 45 2 20 19 67 18 67 3 40 10 76 86 86 我想导出类似的东西,rank列是通过查找“max”列值来派生的,它与“col_1”、“col_2”、“col_3”、“col_4”等列相对应。它应该返回索引或列号,从1开始
ID Col_1 col_2 col_3 col_4 max
1 34 23 45 32 45
2 20 19 67 18 67
3 40 10 76 86 86
我想导出类似的东西,rank列是通过查找“max”列值来派生的,它与“col_1”、“col_2”、“col_3”、“col_4”等列相对应。它应该返回索引或列号,从1开始表示列1,从2开始表示列2,依此类推
ID Col_1 col_2 col_3 col_4 max rank
1 34 23 45 32 45 3
2 20 19 67 18 67 3
3 40 10 76 86 86 4
这是我尝试过的,但无法获得所需的输出。
任何帮助都将不胜感激
SELECT ID,Col_1,col_2,col_3,col_4,max, match(max) AGAINST(col_1,col_2,col_3,col_4) from demo;
这里有一种方法,相当残忍:
select d.*,
(case greatest(col_1, col_2, col_3, col_4, col_5)
when col_1 then 1
when col_2 then 2
when col_3 then 3
when col_4 then 4
when col_5 then 5
end) as max_index
from demo d;
值得注意的是:需要执行这种类型的操作表明数据模型存在缺陷。列值可能应该存储在每行有一个这样的值的表中
我还应该注意,您可以使用field()
:
这在计算上是同样的努力,但长度肯定要短得多。这里有一种方法,那就是蛮力:
select d.*,
(case greatest(col_1, col_2, col_3, col_4, col_5)
when col_1 then 1
when col_2 then 2
when col_3 then 3
when col_4 then 4
when col_5 then 5
end) as max_index
from demo d;
值得注意的是:需要执行这种类型的操作表明数据模型存在缺陷。列值可能应该存储在每行有一个这样的值的表中
我还应该注意,您可以使用field()
:
这在计算上是相同的,但长度肯定要短得多