2013-05-01 63 views
64

我有以下查詢SQL:如何執行字符串不等於

SELECT * FROM table 
WHERE tester <> 'username'; 

我期待這個返回所有結果,其中測試儀是不是字符串username,但這不工作。我想我正在尋找Like運營商的反面,但我不確定?在我的搜索中,我找到了數字解決方案(這是我從中得到的<>),但這似乎不適用於字符串。

+4

您有與'NULL'問題的值值? ('NULL <>'username'' =>'NULL' => false)? – Wrikken 2013-05-01 18:59:42

回答

102

where子句將返回那裏tester不匹配username並在tester不爲空的所有行。

如果你想包括NULL,請嘗試:

where tester <> 'username' or tester is null 

如果您正在尋找不包含單詞「用戶名」作爲子字符串,然後like可用於:

where tester not like '%username%' 
+0

你能否請這個問題的建議https://stackoverflow.com/questions/47549379/how-to-compare-varchar-fields-in-mysql – davidb 2017-11-30 08:13:35

+0

作品。有用! – YumYumYum 2018-01-29 10:01:52

25

嘗試以下查詢

select * from table 
where NOT (tester = 'username') 
5
select * from table 
where tester NOT LIKE '%username%'; 
5

strcomp功能可適當位置(返回0時字符串是相同的):

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

NULL安全的條件下會看起來像:

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