SQL: How to perform string does not equal

Mysql

Mysql Problem Overview


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 Like operator 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.

Mysql Solutions


Solution 1 - Mysql

Your where clause will return all rows where tester does not match username AND where tester is not null.

If you want to include NULLs, try:

where tester <> 'username' or tester is null

If you are looking for strings that do not contain the word "username" as a substring, then like can be used:

where tester not like '%username%'

Solution 2 - Mysql

Try the following query

select * from table
where NOT (tester = 'username')

Solution 3 - Mysql

NULL-safe condition would looks like:

select * from table
where NOT (tester <=> 'username')

Solution 4 - Mysql

select * from table
where tester NOT LIKE '%username%';

Solution 5 - Mysql

The strcomp function may be appropriate here (returns 0 when strings are identical):

 SELECT * from table WHERE Strcmp(user, testername) <> 0;

Solution 6 - Mysql

Another way of getting the results

SELECT * from table WHERE SUBSTRING(tester, 1, 8)  <> 'username' or tester is null

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan Ciborowski - MSFTView Question on Stackoverflow
Solution 1 - MysqlGordon LinoffView Answer on Stackoverflow
Solution 2 - MysqlChrisView Answer on Stackoverflow
Solution 3 - MysqlViktor ZemanView Answer on Stackoverflow
Solution 4 - MysqlÖmer Faruk AlmalıView Answer on Stackoverflow
Solution 5 - Mysqluser3088463View Answer on Stackoverflow
Solution 6 - Mysqlkarthik kasubhaView Answer on Stackoverflow