【问题标题】:SQL: How to perform string does not equalSQL:如何执行字符串不等于
【发布时间】:2013-04-25 19:49:38
【问题描述】:

我有以下问题

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

我希望这会返回 tester 不是字符串 username 的所有结果,但这不起作用。我想我正在寻找 Like 运算符的倒数,但我不确定?在我的搜索中,我找到了数字的解决方案(这就是我得到 的地方),但这似乎不适用于字符串。

【问题讨论】:

  • NULL 值是否存在问题? (NULL &lt;&gt; 'username' => NULL => 假)?

标签: mysql


【解决方案1】:

您的where 子句将返回testerusername 不匹配且tester 不为空的所有行。

如果您想包含 NULL,请尝试:

where tester <> 'username' or tester is null

如果您正在寻找不包含单词“用户名”作为子字符串的字符串,则可以使用like

where tester not like '%username%'

【讨论】:

【解决方案2】:

试试下面的查询

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

【讨论】:

    【解决方案3】:

    NULL 安全条件如下所示:

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

    【讨论】:

    • 是的!这是唯一对我有用的东西,因为我有一个和链。不知道 运算符。谢谢!
    • 刚刚注意到&lt;=&gt; 运算符只存在于MySQL 世界中,更多信息请参见what is <=>
    【解决方案4】:
    select * from table
    where tester NOT LIKE '%username%';
    

    【讨论】:

      【解决方案5】:

      strcomp 函数可能适用于此处(当字符串相同时返回 0):

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

      【讨论】:

        【解决方案6】:

        获取结果的另一种方式

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

        【讨论】:

          猜你喜欢
          • 2019-09-08
          • 1970-01-01
          • 1970-01-01
          • 2012-08-22
          • 2017-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多