doujian3401 2014-02-24 19:32
浏览 5
已采纳

多值查询以查找最近匹配的行[关闭]

I'm looking to create a query that returns the nearest matching row for a set of values. The values are typically between 1-3 (one is 1-5).

I'm not sure the best way to approach right this script. I've had some people say that using distance is the way, and I've had others say that using where/or is the way. Basically the result doesn't have to be an exact match, it just has to list the results in rank of closeness.

The form on this page here is basically what I'm trying to replicate. If anyone could shed light on the approach to the query that I should take that would be very much appreciated.

  • 写回答

1条回答 默认 最新

  • douhuan1908 2014-02-24 19:57
    关注

    Generate a calculated overall "distance" by getting sum the absolute value of the selected number minus the column value for all columns.

    SELECT id, ABS(5-col1)+ABS(5-col2)+ABS(5-col3) as distance
    FROM sometable
    ORDER BY distance ASC
    

    where 5 is the value you are comparing against.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法