當前位置:首頁 » 編程語言 » sql相似度查詢
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql相似度查詢

發布時間: 2023-02-24 03:24:13

sql 怎麼查詢與條件80%相似的數據

SQL> create table ttb(name varchar2(10));

Table created.

SQL> insert into ttb values('asdfjkl');

1 row created.

SQL> insert into ttb values('asdkb');

1 row created.

SQL> insert into ttb values('asd323s');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from ttb;

NAME
------------------------------
asdfjkl
asdkb
asd323s

SQL> select wm_concat(name) from ttb;

WM_CONCAT(NAME)

asdfjkl,asdkb,asd323s

SQL> with t as (select to_char(wm_concat(name)) a from ttb) select substr(y.a,1,x.lvl-1) returnvalue from (select distinct(lvl) lvl from (select rn,first_value(lvl) over(order by lvl) lvl,cnt from (select rn,lvl,count(distinct(str)) cnt from (select rn,level lvl,substr(str,1,level) as str from (select rn,str from (select rn,regexp_substr(a,'[^,]+',1,level,'i') as str from (select rownum rn,a from t) connect by level<=length(a)-length(regexp_replace(a,',',''))+1)) connect by level<=length(str)) group by rn,lvl order by lvl asc) where cnt<>1)) x left join t y on 1=1;

RETURNVALUE

② sql怎麼查詢一列數據的相似度

with t as (select 'xspasdfdfsdfz,xpaysdfsdss,xparsdfd' a from al)
selectsubstr(y.a,1,x.lvl-1) returnvalue from (selectdistinct(lvl) lvl from (select rn,first_value(lvl) over(orderby lvl) lvl,cnt from (select rn,lvl,count(distinct(str)) cnt from (select rn,level lvl,substr(str,1,level) as str from (select rn,str from (select rn,regexp_substr(a,'[^,]+',1,level,'i') as str from (selectrownum rn,a from t) connectby
level<=length(a)-length(regexp_replace(a,',',''))+1)) connectbylevel<=length(str)) groupby rn,lvl orderby lvl asc) where cnt<>1)) x leftjoin t y on1=1;

RETURNVALUE
--------------------------------------------------------------------------------
x

SQL> with t as (select 'xxxxasdfdfsdfz,xxxxxysdfsdss,xxxxxxxxsdfd' a from al)
select substr(y.a,1,x.lvl-1) returnvalue from (select distinct(lvl) lvl from (select rn,first_value(lvl) over(order by lvl) lvl,cnt from (select rn,lvl,count(distinct(str)) cnt from (select rn,level lvl,substr(str,1,level) as str from (select rn,str from (select rn,regexp_substr(a,'[^,]+',1,level,'i') as str from (select rownum rn,a from t) connect by
3 level<=length(a)-length(regexp_replace(a,',',''))+1)) connect by level<=length(str)) group by rn,lvl order by lvl asc) where cnt<>1)) x left join t y on 1=1
4 ;

RETURNVALUE
--------------------------------------------------------------------------------
xxxx

這個例子 'xxxxasdfdfsdfz,xxxxxysdfsdss,xxxxxxxxsdfd' 是把這些放在一行裡面 如果是一列數據 可以把列轉成行
SQL> select * from tt;

ID NAME
---------- ------------------------------
1 aab
1 aac
2 ddca
2 ddcp
2 ddco

SQL> select id,wm_concat(name) newstr from tt group by id;

ID NEWSTR
---------- --------------------
1 aab,aac
2 ddca,ddco,ddcp

這樣把最上面的SQL寫成函數,直接用函數來處理各分組裡面相似度

③ sql 查詢一個表內某一欄位相似數據

可以用like模糊查詢:

select*fromAwherechecklike'我是中國人我來自%'

④ SQL裡面如何計算兩列地址(中文)的相似度

相似度公式:Kq*q/(Kq*q+Kr*r+Ks*s)(Kq>0,Kr>=0,Ka>=0)設q是字元串1和字元串2中都存在的單詞的總數,s是字元串1中存在,字元串2中不存在的單詞總數,r是字元串2中存在,字元串1中不存在的單詞總數,t是字元串1和字元串2中都不存在的單詞總數。

createFUNCTION[dbo].[F_strcompare](@str1VARCHAR(8000),
@str2VARCHAR(8000))
RETURNSVARCHAR(10)
AS
BEGIN
DECLARE@iINT,
@jINT,
@kINT,
@retVARCHAR(10)

SELECT@i=MAX(strlen),
@j=MIN(strlen),
@k=0
FROM(SELECTstrlen=Len(@str1)
UNION
SELECTstrlen=Len(@str2))t

IF(@j=0)
RETURN'0'

WHILE@j>0
BEGIN
IFSubstring(@str1,@j,1)=Substring(@str2,@j,1)
SET@k=@k+1

SET@j=@j-1
END

SET@ret=Rtrim(CAST(@k*100.0/@iASNUMERIC(3,0)))

RETURN@ret
END

⑤ mysql相似度查詢語句

如果僅僅通過SQL語句就要達到這個效果很難了。

我的思路是將查詢的關鍵字,先拆分成一個一個的單個字元。
比如 「我在中國」 。

SQL語句就變成
select 欄位名 from 表名 where 欄位名 like '%我%' or 欄位 like '%在%' or 欄位 like '%中%' or 欄位 like '%國%' .

不過,在mysql中怎樣將這個的一個字元串切割成單個字元,並形成這樣一個sql語句呢,本人比較笨,不曉得咋弄。或許存儲過程能夠實現。

我也看了下mysql的字元串的處理函數。沒有太大頭緒。。
這里是地址,也許對你有用。
http://apps.hi..com/share/detail/7841605

然後呢。我還是建議你先在程序上將關鍵字拆分。再代入sql中進行查詢。

⑥ sql如何高效的比對兩個字元串的相似度

如表格A中欄位a1有一條字元串記錄Rec1內容為 「我的家在哪裡?」;然後要從B表中找出一條字元串記錄Rec2內容與Rec1最相似的,如:「我的家在浙江?」 select A.a1, max(DIFFERENCE(A.a1,B.相應欄位)) from A cross join B group by A.a1

⑦ MYSQL 查詢數據 的相似度

封裝一個存儲過程,把ahuudf拆成一個個位元組,設定一個權值,當作匹配度。通過 _ * 作為來構造like條件。將結果集存儲,然後將最後的結果集進行適當的處理。比如去重之類的。個人想法。沒試過。還憑樓主自己試試

⑧ SQL相近記錄搜索

可能單獨有個類處理排序的(也可能是存儲過程)!不一定是在資料庫里排的!
因為還有一個相似度驗證!這是數字信息處理范疇!

還有一個不是很優化的方法就是你在execute sql前,把你的字元組合裝入一個數組!然後while一下這個數組,生成相應的SQL,然後用union連接各部分SQL

不過這樣做會有重復記錄,而且效率也不高!需要你再手工處理!還不如第一種方法好!先把記錄檢出來,然後通過單獨的一個線程處理排序

⑨ sql 匹配相似記錄

不知道你的相似度判斷的規則是什麼,通過資料庫實現的話,可以考慮定義一個相似度比較函數,然後在update時調用並判斷,下面是MSSQL的思路,不過需要把相似度判斷的函數寫完整。
其他資料庫應該也可以用相同思路來做吧。
FUNCTION fn_GetSimilar
(
@strA As Varchar(255), --傳入的待比較的字元
@StrB As Varchar(255 --期望字元
)
RETURNS int
AS
BEGIN
DECLARE @Result int;
if @strA= @StrB
begin
set @Result=100
end
----
--相似度判斷條件及判斷方法賦值
----
RETURN @Result
END

如果函數測試沒問題,就可以調用下面的方法來修改了
update 不規則名稱表 set StdMedName=b.CommonName from 標准名稱表 b
where dbo.fn_GetSimilar( 不規則名稱表.IrrMedName,b.CommonName)>相似度的值

⑩ sql怎麼查詢一列數據的相似度

SQL> create table ttb(name varchar2(10));

Table created.

SQL> insert into ttb values('asdfjkl');

1 row created.

SQL> insert into ttb values('asdkb');

1 row created.

SQL> insert into ttb values('asd323s');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from ttb;

NAME
------------------------------
asdfjkl
asdkb
asd323s

SQL> select wm_concat(name) from ttb;

WM_CONCAT(NAME)
--------------------------------------------------------------------------------
asdfjkl,asdkb,asd323s

SQL> with t as (select to_char(wm_concat(name)) a from ttb) select substr(y.a,1,x.lvl-1) returnvalue from (select distinct(lvl) lvl from (select rn,first_value(lvl) over(order by lvl) lvl,cnt from (select rn,lvl,count(distinct(str)) cnt from (select rn,level lvl,substr(str,1,level) as str from (select rn,str from (select rn,regexp_substr(a,'[^,]+',1,level,'i') as str from (select rownum rn,a from t) connect by level<=length(a)-length(regexp_replace(a,',',''))+1)) connect by level<=length(str)) group by rn,lvl order by lvl asc) where cnt<>1)) x left join t y on 1=1;

RETURNVALUE
--------------------------------------------------------------------------------
asd