What is the difference between != and <> operators?

后端 未结 4 359
旧巷少年郎
旧巷少年郎 2021-01-17 11:09

I don\'t like not knowing this as there may be situations when I need to use one instead of the other. It seems in most cases they produce the same results but I am taking a

相关标签:
4条回答
  • 2021-01-17 11:20

    From the manual:

    Note: The != operator is converted to <> in the parser stage. It is not possible to implement != and <> operators that do different things.

    So no, there is no difference between the two.

    0 讨论(0)
  • 2021-01-17 11:30

    According to this: http://www.postgresql.org/docs/current/static/functions-comparison.html The operator != gets transformed to <> in the parse stage.

    0 讨论(0)
  • 2021-01-17 11:35

    <> is the standard SQL operator meaning "not equal". Many databases, including postgresql, supports != as a synonym for <>.

    They're exactly the same in postgresql. See also the documentation.

    Note though, that postgresql allows you to implement your own types and overload operators for those types, so ultimately it depends on the datatypes involved what the != and <> operator actually does, but <> and != can never do different things.

    0 讨论(0)
  • 2021-01-17 11:40

    Being curious I found out that the conversion is defined in /src/backend/parser/scan.l:

    /* Convert "!=" operator to "<>" for compatibility */
    if (strcmp(yytext, "!=") == 0)
        yylval->str = pstrdup("<>");
    else
        yylval->str = pstrdup(yytext);
    return Op;
    

    scan.l is used almost at the beginning of parsing. There is also note in Create Operator chapter:

    The operator != is mapped to <> on input, so these two names are always equivalent.

    You can find <> operator in pg_operator table, but there is nothing for !=:

    select * from pg_operator where oprname = '<>';
    select * from pg_operator where oprname = '!=';
    

    There is a lot on this topic in this similar question.

    0 讨论(0)
提交回复
热议问题