Tutorial :Single Equals in MYSQL



Question:

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.


Solution:1

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 :=


Solution:2

More like historical.

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


Solution:3

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."


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »