Reputation: 512
I was running through some of our code at work and came across this structure for an sql query and was sure it was a typo in the variables of our PHP, I ran it and it works.
Select column from table where value = column;
Anyone I know was always taught the correct syntax is:
Select column from table where column = value;
Is there any reason for this being legal, other than SQL just checks are both sides of an equation equal to each other? I'm more posting this because I found it really interesting, like a 'the more you know' kind of thing.
Upvotes: 4
Views: 221
Reputation: 441
The syntax of an SQL query is :
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;
You can use any condition you want, for example :
SELECT * FROM table WHERE 1
will return you all the rows
while
SELECT * FROM table WHERE 0
will return you nothing
after a WHERE you must have a condition, nothing else
Upvotes: 1
Reputation: 312116
The equality operator (=
) is symmetric - if a=b
is true, then so is b=a
. a
and b
can be column names, values or complex expressions. It's common to use the form column=value
, but syntactically speaking, it's completely equivalent to value=column
.
Upvotes: 4
Reputation: 3523
Yes, you have it right sql just checks both sides of an equation. The equation could even contain a column on neither side! such as
SELECT column from table where 1=2;
Upvotes: 1