0

我有一个表,将用户输入的名称存储到文本编辑框中。

此输入框上没有选择或屏蔽

这导致数据库有不一致的数据,例如

John Doe
 John Dow
Jonh doe
johh Doe

当名字应该是 John Doe 时。

我想知道如何创建一个查询,该查询可以使用多个用户名将名称解析回它们应该是的(我知道所有拼写错误的名称出现)。

例子:

select name from tableName
set
(
if(name = 'John Dow' or name = 'Johh Doe') name = 'John Doe'
)

(
if(name = 'Janee Doe' or name = 'Jaan Doe' name = 'Jane Doe'
)

使用 SQL 和 Oracle Developer

4

4 回答 4

1

好吧,您可以使用case

update tableName
    set name = (case when name in ('John Dow', 'Johh Dow') then 'John Doe'
                     when name in ('Janee Doe', 'Jaan doe') then 'Jane Doe'
                     else name
                end)

您可能希望包含一个where子句来限制使用相同值更新的行数:

    where name in ('John Dow', 'Johh Dow', 'Janee Doe', 'Jaan doe')
于 2013-02-19T20:19:40.363 回答
0

这是一个简单的左外连接;假设您将所有拼写错误都放在一个表格中,并且它在拼写错误本身上是唯一的,即每个拼写错误都与一个且只有一个名称相关联。只需确保您首先使用 NVL 取正确的名称

select nvl(b.name, a.name) as name
  from tablename a
  join misspellings b
    on a.name = b.misspelling

我不确定我是否会更新表格(如果您正在尝试这样做并不清楚)如果拼写错误实际上是正确的会发生什么?

于 2013-02-19T20:20:19.833 回答
0

试一试:

    with testdata as
(
select 'John     Doe' as name from dual
union
select ' John dow' as name from dual
union
select ' JON DOH ' as name from dual
union
select ' Joe   wtf ' as name from dual
),
transdata as (
select 'JOHN DOW' as badval, 'JOHN DOE' as goodval from dual
union
select 'JON DOH' as badval, 'JOHN DOE' as goodval from dual
)
select 
'"' || td.name || '"' as raw_name,
--initcap(trim(regexp_replace(nvl(tr.goodval, td.name), '(\W){2,}', ' '))) as output
initcap(nvl(tr.goodval, trim(regexp_replace(td.name, '(\W){2,}', ' ')))) as output
from testdata td, transdata tr
where upper(trim(regexp_replace(td.name, '(\W){2,}', ' '))) = tr.badval(+);


RAW_NAME,OUTPUT
" John dow",John Doe
" JON DOH ",John Doe
"John     Doe",John Doe
" Joe   wtf ",Joe Wtf

确保外部连接到您的翻译表。尽管如此,您也可以尝试对输入进行一些基本的清理。

于 2013-02-19T21:02:02.383 回答
0

忘记连接、initcaps、CASE 等……这些通常不用于搜索文本。使两边都在上面并使用 Like 运算符。复制/粘贴以查看结果:

With t AS
(
 SELECT 'John Doe' name FROM dual
  UNION ALL
 SELECT  'John Dow' FROM dual
  UNION ALL 
 SELECT 'Jonh doe' FROM dual
  UNION ALL
 SELECT 'johh Doe' FROM dual
)
SELECT name FROM t WHERE Upper(name) Like Upper('%jo%do%') --or '%john%do%' - up to you
/ 

Output: 

NAME
-------------
John Doe
John Dow
Jonh doe
johh Doe
于 2013-02-20T15:05:38.017 回答