2013-06-13 2 views
1

J'essaie de trouver des lignes si la deuxième requête qui n'existe pas dans le premier. Mais je reçois l'erreur 'Vous avez une erreur dans la syntaxe SQL près d'une bonne jointure'.MySQL Joining Résultats des requêtes

(SELECT t1.id AS id, t2.id, t3.id 
FROM table1 t1 
INNER JOIN table2 t2 ON t2.id = t1.id 
INNER JOIN table3 t3 ON t3.id = t2.id 
) a 
RIGHT JOIN 
(SELECT id 
FROM table4 
WHERE col1 IS NOT NULL AND col2 IN (1, 2)) b 
ON a.id = b.id 

Qu'est-ce que je fais de mal dans cette requête? Je vous remercie.

+1

Vous pouvez ajouter un 'SELECT * FROM' en haut. –

Répondre

1

Dans les requêtes de jointure, vous devez placer toutes vos colonnes dans la première instruction de sélection. La deuxième instruction select dans votre requête n'est pas valide.

SELECT 
    t1.id as id, t2.id, t3.id, b.id 
FROM 
    (table1 t1 
inner join table2 t2 on t2.id=t1.id 
inner join table3 t3 on t3.id=t2.id) 
right join table4 b on t1.id = b.id 
WHERE b.col1 IS NOT NULL AND b.col2 IN (1, 2))