gpt4 book ai didi

sql - oracle sql 按偏好搜索

转载 作者:行者123 更新时间:2023-12-04 14:19:03 24 4
gpt4 key购买 nike

获取样本表:

WITH t(val) AS
( SELECT 'my Name' FROM dual
UNION
SELECT 'my name' FROM dual
UNION
SELECT 'my naim' FROM dual
UNION
SELECT 'MY kat' FROM dual
UNION
select 'my katt' from dual
)
SELECT * FROM t;

我需要一个优先输出:

查询应该先在表中搜索完全匹配,如果没有找到则通过小写搜索,如果没有找到则只搜索soundex,。所以最终的输出是这样的:

WHERE val = 'my Name'
OR lower(val) = lower('my Name')
OR soundex(lower(val)) = soundex(lower('my Name'))

应该是:

output
-----
my Name

提前致谢。

最佳答案

按照您的描述进行过滤,然后按该顺序排序,然后获取第一条记录:

WITH t AS
( SELECT 'my Name' as val FROM dual
UNION
SELECT 'my name' FROM dual
UNION
SELECT 'my naim' FROM dual
UNION
SELECT 'MY kat' FROM dual
UNION
select 'my katt' from dual
)
SELECT * FROM
(
SELECT * FROM t
WHERE val = 'my Name'
OR lower(val) = lower('my Name')
OR soundex(lower(val)) = soundex(lower('my Name'))
order by
case
when val = 'my Name' then 1
when lower(val) = lower('my Name') then 2
when soundex(lower(val)) = soundex(lower('my Name')) then 3
end
)
WHERE ROWNUM = 1;

关于sql - oracle sql 按偏好搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19656311/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com