Sql server 对不相似的陈述提出异议
我有一个这样的问题Sql server 对不相似的陈述提出异议,sql-server,Sql Server,我有一个这样的问题 Select distinct student_id,student_name, student_id+'-'+student_name from student_table where student_id='1' and (upper(student_name) not like 'testing%' or upper(student_name) not like '%testing') group by student_id, student_name
Select distinct student_id,student_name, student_id+'-'+student_name from student_table
where student_id='1' and (upper(student_name) not like 'testing%'
or upper(student_name) not like '%testing')
group by student_id, student_name
order by student_name asc
学生id 1包含现场测试数据。
如果在这两种情况下都使用'like'或'not like',则返回记录。基本上不一样似乎不起作用。有什么解决方案吗
样本数据:-
student_id student_name
1 testing data
很容易构造一个与条件
匹配的值,它不像“testing%”或“%testing”
,因为
或
条件,我认为您可能打算使用和
条件,以及和条件,许多可能的值也会同时满足这两个谓词。例如,“abc”的varchar值与“testing%”不同,因为“testing%”
只匹配以字母“testing”开头的值,而且与“%testing”不同,因为'%testing'
只匹配以字母“testing”结尾的值
upper()
方法不会产生任何影响。如果使用区分大小写的排序规则,那么对于upper()
的任何输入值,结果都将是大写,这意味着它不会被任何谓词过滤掉,因为它们都是小写。也就是说,每个值都将既不像'testing%'
,又不像'%testing'
编辑:谢谢你的补充意见,你的要求更清楚了。您不希望值以“testing”开头或结尾,并且希望确保不区分大小写的比较。在这种情况下,您需要的条件是:
where student_id = '1'
and upper(student_name) not like '%TESTING'
and upper(student_name) not like 'TESTING%'
另一方面,有一个“student_id”列,其名称似乎表明它包含数值,但随后将该列与数值的字符版本进行比较,这有点奇怪。你确定你不只是想要
where student\u id=1
而不是where student\u id='1'
?如果看不到表的架构,很难说。为什么要将其转换为大写,然后使用小写字符串文字?@MartinSmith I这样做是为了消除区分大小写的功能。但即使没有它,也无法工作。如果服务器使用区分大小写的排序规则,那么无论是testing%
还是%testing
或testing%
或%testing
都不会像“testing”那样,这就是您最初所说的数据。现在你说它是“测试数据”
。哪一个?要“删除区分大小写”,如果您甚至使用区分大小写的排序规则,将大写字符串与小写字符串进行比较显然是行不通的。为什么是不同的
和分组依据
,为什么是分组依据
,而没有聚合?你明白这两个操作符的作用吗?我想删除任何以开始或以测试结束的数据。因此,我认为或是正确的选择,在这种情况下,你的条件应该类似于,而不是(学生名类似于“testing%”或学生名类似于“%testing”)
,但是你仍然有一个令人费解的upper()
“@下划线\u d no我想找一个不包含testingSo的not(a或b)
与not a和not b
相同。它不同于非a或非b
。正如@underline_d所指出的,“contains”不同于“以开头或以结尾”。例如,值abcdef
包含“cd
,但既不以cd
开头,也不以cd>结尾。所以我又一次搞不清楚到底需要哪一个。用而不是(a或b)
删除记录的工作示例。请随意使用此查询来说明您的答案;你已经涵盖了它背后的逻辑,所以我只是在模仿你。