Инструменты пользователя

Инструменты сайта


programming:database:mysql:join

JOIN tips

This is a change that was made in 5.0.15 to make MySQL more compliant with the standard. According to the SQL:2003

<from clause> ::= FROM <table reference list>
<table reference list> ::=
    <table reference> [ { <comma> <table reference> }... ]
<table reference> ::=
    <table factor>
  | <joined table>
<joined table> ::=
    <cross join>
  | <qualified join>
  | <natural join>
...

Thus when you write

  ... FROM t1 , t2 LEFT JOIN t3 ON (expr)

it is parsed as

(1)    ... FROM t1 , (t2 LEFT JOIN t3 ON (expr))

and not as

(2)    ... FROM (t1 , t2) LEFT JOIN t3 ON (expr)

so, from expr you can only refer to columns of t2 and t3 - operands of the join. Workaround - to put parentheses explicitly as in (2). Then you can refer to t1 columns from expr.

Unfortunately, this change is not properly documented in the manual, it will be fixed.

Еще хитрости про скобки

1. There were TWO different changes in the processing of joins to bring MySQL's behaviour in line with that of SQL:2003.

2. The behaviour change reported by C. Vu is NOT a bug; it is entirely intentional. Queries that used to work with «FROM t1, t2» now need to be written using «FROM (t1, t2)» as previously stated.

3. However, the Manual's explanation as to why this is so in this particular case is incorrect, and will be fixed ASAP.

programming/database/mysql/join.txt · Последнее изменение: 2017/03/27 11:26 — artur

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki