我非常肯定某人已经有了这个问题,但我只是不知道如何写作。
在我的SQL服务器数据库上,我试图搜索其中field1包含abc而不是abc [de]
的记录因此会找到以下记录
'123abc222'
'ABC'
以下记录将无法找到
'123ABC〔脱〕'
我知道
'abc123abc [de]'很棘手,但我现在不用担心。
很多人会问为什么我要执行此搜索而不是更改程序,数据库等。这就是问题所在。程序处于稳定状态的问题。添加额外的字段几乎是不可能的。我们团队想要引入更多问题的最后一件事。是的,添加额外的列或修改数据库设计更好,但是,像大多数真实世界的应用程序一样,我们只想对现有应用程序进行最小的更改
由于
答案 0 :(得分:2)
select *
from t
where field1 like '%abc%'
and field1 not like '%abcde%'
测试设置:http://rextester.com/ZZTZ72327
create table t (
id int identity(1,1)
, field1 varchar(32) not null
);
insert into t values
('123abc222')
,('123abcde');
查询:
select *
from t
where field1 like '%abc%'
and field1 not like '%abcde%'
结果:
+----+-----------+
| id | field1 |
+----+-----------+
| 1 | 123abc222 |
+----+-----------+
答案 1 :(得分:2)
使用like
和not like
:
where col like '%abc%' and col not like '%abcde%'
答案 2 :(得分:1)
类似的东西:
select * from table where field like 'abc%' and field not like '%def%';
答案 3 :(得分:1)
感谢大家的回答。我错了,我没有正确地模拟问题
从jce和SqlZim引用
select *
from t
where field1 like '%abc%'
and field1 not like '%\[abcde]%' ESCAPE '\'
感谢大家的帮助
答案 4 :(得分:0)
如何用空字符替换所有数字,然后使用'='比较器比较结果。
SELECT *
FROM table
WHERE field = REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
REPLACE(@str, '0', ''),'1', ''),'2', ''),
'3', ''),'4', ''),'5', ''),'6', ''),'7',
''),'8', ''),'9', '')
这样只会让你输掉字母字符。