MySQL SQL:如何执行字符串不等于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16324504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL: How to perform string does not equal
提问by Dan Ciborowski - MSFT
I have the following query
我有以下查询
SELECT * FROM table
WHERE tester <> 'username';
I am expecting this to return all the results where tester is not the string username, But this not working. I think I am looking for the inverse of the Likeoperator but I am not sure? In my searches I have found solutions for numbers (that's where i got <> from), but this seems to not be working with strings.
我希望这会返回 tester 不是 string 的所有结果username,但这不起作用。我想我正在寻找Like运算符的逆,但我不确定?在我的搜索中,我找到了数字的解决方案(这是我得到 <> 的地方),但这似乎不适用于字符串。
回答by Gordon Linoff
Your whereclause will return all rows where testerdoes not match usernameAND where testeris not null.
您的where条款将返回所有的行tester不匹配,username并在tester不为空。
If you want to include NULLs, try:
如果要包含 NULL,请尝试:
where tester <> 'username' or tester is null
If you are looking for strings that do not contain the word "username" as a substring, then likecan be used:
如果您正在寻找不包含“用户名”作为子字符串的字符串,则like可以使用:
where tester not like '%username%'
回答by Chris
Try the following query
试试下面的查询
select * from table
where NOT (tester = 'username')
回答by Viktor Zeman
NULL-safe condition would looks like:
NULL 安全条件如下所示:
select * from table
where NOT (tester <=> 'username')
回答by ?mer Faruk Almal?
select * from table
where tester NOT LIKE '%username%';
回答by user3088463
The strcompfunction may be appropriate here (returns 0 when strings are identical):
该strcomp函数可能适用于此处(当字符串相同时返回 0):
SELECT * from table WHERE Strcmp(user, testername) <> 0;
回答by karthik kasubha
Another way of getting the results
获取结果的另一种方式
SELECT * from table WHERE SUBSTRING(tester, 1, 8) <> 'username' or tester is null

