Google Query中的确切结果,如果不存在确切结果则会部分匹配

时间:2017-06-26 03:33:03

标签: google-sheets google-query-language

我使用Google查询搜索Google电子表格:https://jsfiddle.net/BaronGrivet/ctkpvuac/3/

示例数据:

Block Hill Road
Greenhill Road
Hill Road
Brownhill Road

示例查询:

'SELECT A WHERE LOWER(A) LIKE "%'+searchInput+'%" ORDER BY A ASC'

如果有人搜索"希尔"它将返回所有行。

然而,如果有人搜索" Hill Road"它仍将返回所有的行与#34; Hill Road"最后列出。

理想情况下,我希望它只返回完全匹配" Hill Road",或至少有" Hill Road"作为第一个回应。

有没有办法构建查询,以便只显示或首先显示完全匹配?

或者我是否必须编写一个后备内容,我首先查询完全匹配,如果没有返回,我会查询部分匹配?

1 个答案:

答案 0 :(得分:1)

也许是朝着正确方向迈出的一步:

={query(A:A,"select * where LOWER(A) LIKE 'hill road%' ORDER BY A desc");query(A:A,"select * where LOWER(A) LIKE '%hill road%' ORDER BY A desc")} 

发出命令:

Hill Road
Hill Road
Greenhill Road
Brownhill Road
Block Hill Road

(完全匹配重复),或者可能在ColumnA中为所有条目添加空格并应用:

={query(A:A,"select * where LOWER(A) LIKE '% hill road%' ORDER BY A desc");query(A:A,"select * where LOWER(A) LIKE '%hill road%' ORDER BY A desc")}

给予:

 Hill Road
 Block Hill Road
 Hill Road
 Greenhill Road
 Brownhill Road
 Block Hill Road

如果你不想让'exact'和'similar'重复,那么请用UNIQUE包装:

(a)没有增加空格:

=unique({query(A:A,"select * where LOWER(A) LIKE '% hill road%' ORDER BY A desc");query(A:A,"select * where LOWER(A) LIKE '%hill road%' ORDER BY A desc")})

输出:

Hill Road
Greenhill Road
Brownhill Road
Block Hill Road

(b)增加空格:

=unique({query(A:A,"select * where LOWER(A) LIKE '% hill road%' ORDER BY A desc");query(A:A,"select * where LOWER(A) LIKE '%hill road%' ORDER BY A desc")})

输出:

 Hill Road
 Block Hill Road
 Greenhill Road
 Brownhill Road

如果选择UNIQUE订单asc可能会更有意义。