我正在嘗試編寫一個查詢,該字符串給出兩個字符串之間不匹配的字母總數。查找兩個字符串之間不匹配的字母
例如,我已經給定的兩個字符串
串1:詹姆斯的字符串2:Romeeo
我需要找出沒有第二個字符串中的字母總數在第一個字符串中有匹配。
字母是
R,O,O-和e
(注意,第一個字符串只有一個E,所以在Romeeo額外的E不具有匹配在字符串1)。
總之,那些字母(R,O,O,以及e)在串1
不存在有沒有去解決的Oracle SQL這個問題呢?
我正在嘗試編寫一個查詢,該字符串給出兩個字符串之間不匹配的字母總數。查找兩個字符串之間不匹配的字母
例如,我已經給定的兩個字符串
串1:詹姆斯的字符串2:Romeeo
我需要找出沒有第二個字符串中的字母總數在第一個字符串中有匹配。
字母是
R,O,O-和e
(注意,第一個字符串只有一個E,所以在Romeeo額外的E不具有匹配在字符串1)。
總之,那些字母(R,O,O,以及e)在串1
不存在有沒有去解決的Oracle SQL這個問題呢?
有趣的益智遊戲;)
使用分析功能COUNT()
並通過劃分到當前行,你實際上能夠「爲你的字母編號」:
SELECT letters,
COUNT(*) OVER (PARTITION BY letters ORDER BY n ROWS
BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) cnt FROM (
-- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SELECT SUBSTR('Jameess', LEVEL, 1) letters, LEVEL n FROM DUAL
CONNECT BY LEVEL <= LENGTH('Jameess')
)
生產這一結果:
LETTERS CNT
J 1 -- first J
a 1 -- first a
e 1 -- first e
e 2 -- second e
m 1 -- ...
s 1
s 2
做一次每個字符串,你只需要比較每個字母索引它自己的組:
SELECT s2.letters
FROM (
SELECT letters,
COUNT(*) OVER (PARTITION BY letters ORDER BY n ROWS
BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) cnt FROM (
SELECT SUBSTR('Jameess', LEVEL, 1) letters, LEVEL n FROM DUAL
CONNECT BY LEVEL <= LENGTH('Jameess')
)
) S1
RIGHT OUTER JOIN (
SELECT letters,
COUNT(*) OVER (PARTITION BY letters ORDER BY n ROWS
BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) cnt FROM (
SELECT SUBSTR('Romeeeeo', LEVEL, 1) letters, LEVEL n FROM DUAL
CONNECT BY LEVEL <= LENGTH('Romeeeeo')
)
) S2
ON s1.letters = s2.letters AND s1.cnt = s2.cnt
WHERE s1.cnt IS NULL
-- ^^^^^^
-- change to `s2.cnt` to compare your strings the other way around
-- and replace the RIGHT JOIN by a LEFT JOIN
ORDER BY letters
生產:
LETTERS
R
e
e
o
o
(出於測試目的,我添加了一些額外的e
在Jameess
和Romeeeeo
)
試試這個SQL查詢
DECLARE @string1 VARCHAR(100)='Jamess'
DECLARE @string2 VARCHAR(100)='Romeeo'
DECLARE @Notmatchstring VARCHAR(100)
SET @Notmatchstring =''
DECLARE @index INT
DECLARE @count INT
SET @count=1
WHILE @count <= LEN(@string2)
BEGIN
SET @index=CHARINDEX(SUBSTRING(@string2,@count,1),@string1,0)
IF(@index=0)
BEGIN
SET @Notmatchstring [email protected] +' '+SUBSTRING(@string2,@count,1)
END
IF(@index>0)
BEGIN
SET @string1=REPLACE(@string1,SUBSTRING(@string2,@count,1),'')
END
SET @[email protected]+1
END
SELECT @Notmatchstring as NotMatchingCharacter
這個問題清楚地表明,使用的數據庫是Oracle,而不是SQL Server。 [SQL]標記是指結構化查詢語言,而不是微軟的SQL Server產品。 – 2014-10-22 11:11:23
在Oracle,
SQL> WITH DATA AS
2 (SELECT 'Jamess' str1, 'Romeeo' str2 FROM dual
3 ),
4 data2 AS
5 (SELECT SUBSTR(str1, LEVEL, 1) str1
6 FROM DATA
7 CONNECT BY LEVEL <= LENGTH(str1)
8 ),
9 data3 AS
10 (SELECT SUBSTR(str2, LEVEL, 1) str2
11 FROM DATA
12 CONNECT BY LEVEL <= LENGTH(str2)
13 )
14 SELECT * FROM data3 WHERE str2 NOT IN
15 (SELECT str1 FROM data2
16 )
17 UNION ALL
18 SELECT str2
19 FROM data3
20 WHERE str2 IN
21 (SELECT str1 FROM data2
22 )
23 GROUP BY str2
24 HAVING COUNT(*)>1
25/
S
-
R
o
o
e
SQL>
問題被加上[plsql]
所以我想一個PL/SQL的解決方案是爲了:
DECLARE
stringA VARCHAR2(20) := 'Jamess';
stringB VARCHAR2(20) := 'Romeeo';
strDiff VARCHAR2(20);
FUNCTION find_unmatched(p1 IN VARCHAR2, p2 IN VARCHAR2)
RETURN VARCHAR2
IS
s1 VARCHAR2(32767) := p1;
s2 VARCHAR2(32767) := p2;
s3 VARCHAR2(32767);
c CHAR(1);
p NUMBER;
BEGIN
LOOP
c := SUBSTR(s2, 1, 1);
p := INSTR(s1, c);
IF p = 0 THEN -- c not found in s1: add to unmatched list and remove from s2
s3 := s3 || c;
s2 := SUBSTR(s2, 2);
ELSE -- c found in s1: remove from s1 and s2
s1 := SUBSTR(s1, 1, p-1) || SUBSTR(s1, p+1, LENGTH(s1)-p);
s2 := SUBSTR(s2, 2);
END IF;
IF s1 IS NULL OR s2 IS NULL THEN
EXIT;
END IF;
END LOOP;
RETURN s3;
END find_unmatched;
BEGIN
strDiff := find_unmatched(stringA, stringB);
DBMS_OUTPUT.PUT_LINE('strDiff=''' || strDiff || '''');
END;
分享和享受。
優秀的解決方案 – user311509 2014-10-22 15:58:19