views:

254

answers:

3

Hi, I was wondering why MYSQL uses a single equals sign in conditional statements instead of the more typical two equals signs. Is there a technical/historical reason for this? Thanks.

+6  A: 

More like historical.

It's SQL. It has used a single equals sign for comparison since the early '70s.

Guffa
Any idea of the source?
William
It was developed by IBM in the '70s. It was originally named Structured English Query Language, so it's intended to be close to regular human language.
Guffa
+3  A: 

Hi, I was wondering why MYSQL uses a single equals sign in conditional statements instead of the more typical two equals signs. Is there a technical/historical reason for this? Thanks.

Comparison is much more common in SQL than assignment.

That's why SQL uses more short syntax to do more common things.

In classical SQL, comparison can be distinguished from assignment by context (assignment can be only in SET clause of an UPDATE statement), that's why one operator can be used for both operations.

In MySQL's extension to SQL, assignment to a session variable is denoted by :=

SQL evolved from QUEL used by Ingres (which is the grandfather of many contemporary RDBMS's), which used = sign too.

Quassnoi
+3  A: 

There is never a case for ambiguity in SQL.

In the original A Guide to the SQL Standard by C.J.Date (1987 edition), = for assignment is only used in the SET clause of UPDATE. Everywhere else = is used it is used for comparison.

But in other languages, such as C/C++/C#/Java, = can be used as assignment but it also returns a value. So a = b means "set a equal to b, and return a" whereas a == b means "return true if a and b are equal". (This leads to a very common bug in C programs, because if (a = b) and if (a == b) are both valid, since the result doesn't have to be a bool.)

Some languages like JavaScript/ECMAScript also introduce === as a third type of comparison. In those languages, == means "convert to same type and compare" whereas === means "return true only if they are the same type and same value."

lavinio