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

27,823

Solution 1

They are both exactly the same. See the documentation.

http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_not-equal

Solution 2

<> should be preferred, all things being equal, since it accords with the sql standard and is technically more portable...

!= is non-standard, but most db's implement it.

sql:2008 grammar:

<not equals operator> ::=
  <>

Solution 3

No difference. <> is sql standard, != non-standard.

Solution 4

Nothing. Simply two different ways of writing the same thing

Share:
27,823
Alain Tiemblo
Author by

Alain Tiemblo

I am a geek who subscribed there to have fun. Some people do gaming, some poeple do chatting, and I do code. I can answer a lot of questions, but I prefer strange and challenging problems in PHP, Symfony and/or MySQL. About opensource, I developed twigfiddle.com, a website that provides a small development environment to develop, run, store and access Twig code online, the symfony collection jQuery plugin to handle client-side of Symfony collections, and domajax, a jquery plugin built to make ajax interactions easier and cleaner. You'll find me the most on the php, twig, curl and gd tags.

Updated on September 16, 2020

Comments

  • Alain Tiemblo
    Alain Tiemblo over 3 years

    If I use a simple table such as :

    create table test ( a int );
    insert into test values ( 1 ) , ( 2 ) , ( 2 ) , ( 3 );
    select * from test where a <> 2;
    select * from test where a != 2;
    

    Both give me :

    +------+
    | a    |
    +------+
    |    1 |
    |    3 |
    +------+
    2 rows in set (0.00 sec)
    

    So what is the difference between <> and != mysql operators ?

  • Alain Tiemblo
    Alain Tiemblo over 11 years
    Thank you, strange notation. Just seen that on a coworker code and just got stuck.
  • anothershrubery
    anothershrubery over 11 years
    Yeah, while I use C-based languages mainly I tend to use <> even though != feels more natural to me. <> just seems right for any SQL representation.
  • Dennis
    Dennis almost 7 years
    why is it technically more portable?
  • Cave Johnson
    Cave Johnson almost 6 years
    @Dennis I guess because it is sql standard.
  • mingchau
    mingchau almost 5 years
    @Dennis it should be more compatible to use <> and easily to transplant the query in different RDBMS. so it's considering portable?