Anon
Anon

Reputation: 5163

Single Equals in MYSQL

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.

Upvotes: 7

Views: 4533

Answers (3)

Quassnoi
Quassnoi

Reputation: 425643

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

Upvotes: 9

lavinio
lavinio

Reputation: 24309

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

Upvotes: 5

Guffa
Guffa

Reputation: 700592

More like historical.

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

Upvotes: 7

Related Questions