2012-05-07 52 views
12

Tengo la siguiente consulta MySQL:MySQL "Columna desconocida en Sobre la cláusula"

SELECT posts.id, posts.name, LEFT(posts.content, 400), posts.author, posts.date, users.display_name, 
    GROUP_CONCAT(tags.tag ORDER BY tag_linking.pid ASC SEPARATOR ",") update_tags 
FROM posts, tag_linking, tags 
INNER JOIN `users` 
ON posts.author=users.id; 
WHERE tag_linking.pid = posts.id 
    AND tags.id = tag_linking.tid 
ORDER BY posts.date DESC 

cual se haya puede ver, conecta tres mesas, etc., etc. En cualquier caso, el problema es que da un error:

ERROR CODE: 
SQL Error (1054): Unknown column 'posts.author' in 'on clause' 

a pesar de que esta consulta más simple utilizado en otra página funciona:

SELECT posts.id, posts.name, LEFT(posts.content, 400), posts.author, posts.date, users.display_name FROM `posts` 
INNER JOIN `users` 
ON posts.author=users.id 

¿alguien tiene pensamientos de por qué esto es OCC uring? Gracias por tu ayuda.

+1

¿Alguna razón por la que está mezclando 'lazy join' con la sintaxis de 'explícita unión'? –

Respuesta

38

debido a su mezcla sintaxis de unión

From Mysql[docs]

However, the precedence of the comma operator is less than of INNER JOIN, CROSS JOIN, LEFT JOIN, and so on. If you mix comma joins with the other join types when there is a join condition, an error of the form Unknown column 'col_name' in 'on clause' may occur. Information about dealing with this problem is given later in this section.

la solución es:

To allow the join to be processed, group the first two tables explicitly with parentheses so that the operands for the ON clause are (t1,t2) and t3:

SELECT * FROM (t1, t2) JOIN t3 ON (t1.i1 = t3.i3);

Alternatively, avoid the use of the comma operator and use JOIN instead:

SELECT * FROM t1 JOIN t2 JOIN t3 ON (t1.i1 = t3.i3);

1

es que punto y coma usted tiene ON posts.author=users.id;

+0

Disculpa, eso fue un error tipográfico. –

0

Dar un alias a las tablas en from cláusula y surroundin G con paréntesis lo haría funcionar. Pero mezclar combinaciones sigue siendo una mala práctica.

0

Simplemente dice que el autor no existe en la tabla de publicaciones. o un error de ortografía?

Cuestiones relacionadas