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

后端 未结 4 405
旧巷少年郎
旧巷少年郎 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: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.

提交回复
热议问题