MySQL 使用部分精确数学的数据库搜索
MySQL DB search with partial exact math
我有MySQL个人参数数据表(每人约100个):
{"id":"1", "height":"182", "hair_color":"red", "eyes_color":"haze", "day_of_birth":"25"}
(JSON格式只是为了简单起见)
我必须能够实现部分搜索,能够通过 5-1 个参数获得结果。
查询:["height":182, "hair_color":"red", "day_of_birth":"25", "skin_color":"black"]
最后我需要包含 NUMBER 个匹配参数的匹配行列表
赞:["id":"1","matched":"2"]
然后得到,比如说 "first 100" 匹配最多的(当然,尽快 :))
如果您必须坚持使用 sql 数据库,请尝试以下操作:
select id, if (height=182,1,0)
+ if(hair_color='red',1,0)
+ if(day_of_birth=25,1,0)
+ if(skin_color='black',1,0) as matched
from person
order by matched desc
limit 100
然而,为了匹配大量具有大量参数的条目,可能会有更好的选择,例如使用全文索引,例如 Apache Lucene / SOLR。
我有MySQL个人参数数据表(每人约100个):
{"id":"1", "height":"182", "hair_color":"red", "eyes_color":"haze", "day_of_birth":"25"}
(JSON格式只是为了简单起见)
我必须能够实现部分搜索,能够通过 5-1 个参数获得结果。
查询:["height":182, "hair_color":"red", "day_of_birth":"25", "skin_color":"black"]
最后我需要包含 NUMBER 个匹配参数的匹配行列表
赞:["id":"1","matched":"2"]
然后得到,比如说 "first 100" 匹配最多的(当然,尽快 :))
如果您必须坚持使用 sql 数据库,请尝试以下操作:
select id, if (height=182,1,0)
+ if(hair_color='red',1,0)
+ if(day_of_birth=25,1,0)
+ if(skin_color='black',1,0) as matched
from person
order by matched desc
limit 100
然而,为了匹配大量具有大量参数的条目,可能会有更好的选择,例如使用全文索引,例如 Apache Lucene / SOLR。