我需要搜索比实际更大的最接近的拨号代码。例如,最接近 33 的将是 331 或 332(如果 331 不存在)......所以它必须是 33xxxx,34 无效。
这 2 个查询有效,但速度太慢(250 毫秒/行):
SELECT Dialcode
FROM table
WHERE (Dialcode LIKE '$var%' AND Dialcode > '$var' AND Price IS NOT NULL)
ORDER BY Dialcode ASC LIMIT 1
SELECT Dialcode
FROM table
WHERE (MATCH(Dialcode) AGAINST ('$var*' IN BOOLEAN MODE) AND Dialcode > '$var'
AND Price IS NOT NULL)
ORDER BY Dialcode ASC LIMIT 1
我的拨号代码是 PRIMARY KEY BIGINT(15)。
我这样做吗,它真的很快(> 1ms/行),但这并不是我所需要的:
SELECT Dialcode
FROM table
WHERE (Dialcode >= '$var' AND Price IS NOT NULL)
ORDER BY Dialcode ASC LIMIT 1
所以我想我的问题是 LIKE / MATCH AGAINST。
任何想法,将不胜感激。
更新解决方案:
改编自raina77ow建议的解决方案:
SELECT Dialcode FROM table WHERE (( (Dialcode BETWEEN $var * 1 AND ’9’ )
OR (Dialcode BETWEEN $var * 10 AND $var.’99’ )
OR (Dialcode BETWEEN $var * 100 AND $var.’999’ )
OR (Dialcode BETWEEN $var * 1000 AND $var.’9999’ )
…
) AND Price IS NOT NULL) ORDER BY Dialcode ASC LIMIT 1
多谢你们!