简体   繁体   中英

SQL: Expression order in SQL compare

Is the expression order relevant in a compare statement? For example:

select * from person where name = 'John'

and

select * from person where 'John' = name

Is there another reason other than readability to have the column first instead of the value?

Most SQL database systems will have sections of their documentation that explicitly describe their syntax. I'll use SQL Servers Search Condition 1 , since that's my most common reference:

 <search_condition> ::= MATCH(<graph_search_pattern>) | <search_condition_without_match> | <search_condition> AND <search_condition> <search_condition_without_match> ::= { [ NOT ] <predicate> | ( <search_condition_without_match> ) } [ { AND | OR } [ NOT ] { <predicate> | ( <search_condition_without_match> ) } ] [ ...n ] <predicate> ::= { expression { = | < > | ! = | > | > = | ! > | < | < = | ! < } expression

Here, we can see that the items on both side of the = (as well as many other operators) are described as expression - that is, anything that can appear on the left can equally well appear on the right.

Compare this, for example, with IN 2 :

 | expression [ NOT ] IN ( subquery | expression [ ,...n ] )

where we can clearly see that what's allowed on the left and right are subtly different.

It's well worth, when you have question on syntax, to consult the relevant documentation for your database product. For standard SQL, as here, just about any product's documentation will do.


1 Which in turn was reached from the documentation for WHERE

2 Same page as <predicate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM