【问题标题】:Sql Only String search仅 Sql 字符串搜索
【发布时间】:2018-06-05 12:13:03
【问题描述】:
with temp(name) as (select 'abc' from dual
union select '123abc' from dual
union select '1abc3' from dual)
select * from temp where temp.name  like '%[a-z]%'

为什么我无法在输出中获得所有 3 条记录 ,我用的是Oracle SQL Developer 11g

我使用此查询的原因是:我想检查所需列是否仅包含字符串,没有数字和特殊字符

【问题讨论】:

  • 您使用的是什么数据库?请适当标记。
  • 他正在使用 ORACLE。他正在使用双表。

标签: sql string oracle oracle11g


【解决方案1】:

您正在混合使用 SQL Server 和 Oracle 语法:

SQL SERVER Demo

with temp(name) as (
      select 'abc' union all select '123abc'  union select '1abc3' 
)
select * from temp where temp.name like '%[a-z]%';
                                        -- [] is T-SQL specific

ORACLE Demo:

with temp(name) as (select 'abc' from dual
union select '123abc' from dual
union select '1abc3' from dual)
select * from temp where regexp_like(temp.name, '[a-z]')

【讨论】:

  • 你能告诉我Oracle语法吗
【解决方案2】:

首先,您正在混合概念。您正在尝试使用普通的LIKE 运算符进行正则表达式比较。其次,您应该使用您正在使用的数据库标记您的问题。鉴于dual 表,我假设您使用的是oracle

WITH temp(name) AS (SELECT 'abc' FROM dual
    UNION SELECT '123abc' FROM dual
    UNION SELECT '1abc3' FROM dual)
SELECT * FROM temp 
WHERE REGEXP_LIKE(temp.name,'[a-z]+')

另请注意,在正则表达式匹配中使用% 通配符将无法正常工作。

【讨论】:

    【解决方案3】:

    首先,最好使用union all 而不是union,因为union 会产生删除重复项的开销。

    其次,您的like 模式是非标准的,仅在 SQL Server 和 Sybase 中受支持。换句话说,你的名字中没有一个包含方括号或连字符,所以没有一个与模式匹配。

    这是否符合您的预期?

    with temp(name) as (
          select 'abc' from dual union all
          select '123abc' from dual union all
          select '1abc3' from dual
         )
    select *
    from temp
    where temp.name like '%a%';
    

    这只是检查语法。它不等同于您的 like 模式。

    我从您使用 Oracle 的语法推测。如果是这样,那么您可以使用正则表达式:

    where regexp_like(temp.name, '[a-z]')
    

    大多数(但不是全部)数据库都支持正则表达式,因此您可以在大多数数据库中表达类似的逻辑。

    【讨论】:

    • 你的like子句与question like子句不匹配
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-13
    • 1970-01-01
    • 2016-01-11
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多