【发布时间】:2019-10-11 02:37:06
【问题描述】:
我试图在这种情况下从表中获取所有值:
- 长度为3
- 值不在大于 3 的值中
表格示例(simple_table):
id | name
-------------
1 | 418
2 | 223:542
3 | 54
4 | 418
5 | 418:223:100
6 | 223
7 | 999
8 | 132
9 | null
10 | 100
所以我有 三个不同的类长度
- 名称长度为 3
- 名字长度小于3
- 名字长度超过3个
我的代码:
第一
select distinct name
from simple_table
where name is not null
and LENGTH( name ) = 3;
返回值
name
-------
418
223
999
132
第二:
select distinct name
from simple_table
where name is not null
and LENGTH( name ) > 3;
返回值
name
-------
223:542
418:223:100
代码的“主要部分”
select distinct name
from simple_table
where name is not null
and LENGTH( name ) = 3
and '223:542' not like CONCAT(CONCAT('%', name ), '%');
这个返回
name
-------
418
999
132
100
当我尝试使用此代码时,它会导致错误
select distinct name
from simple_table
where name is not null
and LENGTH( name) = 3
and (select distinct name
from simple_table
where name is not null
and LENGTH( name) > 3) not like in (CONCAT(CONCAT('%', name), '%'));
错误:
ORA-00936: missing expression
00936. 00000 - "missing expression"
*Cause:
*Action:
Error at Line: 15 Column: 50
我想要的结果是:
name
-------
999
132
【问题讨论】:
-
如果这是用 t-sql 编写的 - 您必须在最后一行中去掉“IN”运算符 - 不确定这是否适用于 Oracle
-
首先,这不是存储这些数字的好方法,而且这种设计导致了 99% 的问题。其次,你得到的错误来自
LENGTH( name) > 3) not like in (CONCAT(CONCAT('%', name), '%'));这是不正确的语法,但修复它没有什么意义,因为它根本不会做你想要的。为了拆分该字符串,请查看此线程:stackoverflow.com/questions/26878291/…