-2

I have 4 tables: Fonctionnaire, Echelon, PromotionEchelon, Mise.

Fonctionnaire: CodeFonctionnaire(PK), NomFonctionnaire, PrenomFonctionnaire

Echelon : NumEchelon(PK), CodeEchelon, Indice

PromotionEchelon:CodeFonctionnaire(FK),NumEchelon(FK),DateArrete

Mise:CodeMise(PK),Nature,NumArrete,DateArrete,DureeMise,CodeFonctionnaire(FK)

I want to select the list of all 'Fonctionnaire' and their 'DateArrete' from

'PromotionEchelon' and their 'CodeEchelon, Indice' from 'Echelon' and if

exist their 'Nature,NumArrete,DateArrete,DureeMise' from 'Mise'.

I used this query:

SELECT 
Fonctionnaire.CodeFonctionnaire, 
Fonctionnaire.NomFonctionnaire,
Fonctionnaire.PrenomFonctionnaire,
Echelon.CodeEchelon, Echelon.Indice, 
PromotionEchelon.DateArrete,
Mise.Nature, Mise.NumArrete, Mise.DateArrete, Mise.DureeMise 
FROM
Fonctionnaire, PromotionEchelon, Echelon, Mise 
WHERE
Fonctionnaire.CodeFonctionnaire = PromotionEchelon.CodeFonctionnaire 
AND  PromotionEchelon.NumEchelon = Echelon.NumEchelon 
AND Fonctionnaire.CodeFonctionnaire = Mise.CodeFonctionnaire

But it returns 0 records, i tried inner and outer join and i get errors.

So where is the error in my query?

IgorM
  • 1,348
  • 1
  • 12
  • 28
user4340666
  • 1,453
  • 15
  • 36
  • 2
    Build up your query in steps. Start with one table, add a join etc. When does it go wrong? – jarlh Feb 15 '16 at 09:14
  • 1
    It would be easier to read and in some case more efficient if you put the constraints on the joins. Using ANSII Syntax also makes it far easier to read (especially for other developers) – Fred Feb 15 '16 at 09:16
  • I used query with 3 tables Fonctionnaire, PromotionEchelon, Echelon and it gives me result but when i add Mise table it gives me 0 records because there is no Fonctionnaire in Mise. – user4340666 Feb 15 '16 at 09:17
  • when you link any table that has no records, make sure to link it as outer join (left outer join) to avoid having 0 records in the result set. – Boody Feb 15 '16 at 09:23

1 Answers1

0

You do not address the JOIN chain properly

Start with the following and see what you get

    SELECT 
      Fonctionnaire.CodeFonctionnaire
    , Fonctionnaire.NomFonctionnaire
    , Fonctionnaire.PrenomFonctionnaire
    , Echelon.CodeEchelon, Echelon.Indice 
    , PromotionEchelon.DateArrete

     ,Mise.Nature, Mise.NumArrete, Mise.DateArrete, Mise.DureeMise 
        FROM 
        Fonctionnaire f

LEFT JOIN PromotionEchelon p
        ON p.CodeFonctionnaire = f.CodeFonctionnaire

LEFT JOIN Mise m 
        ON m.CodeFonctionnaire = f.CodeFonctionnaire
LEFT JOIN Echelon e
        ON e.NumEchelon = p.NumEchelon
IgorM
  • 1,348
  • 1
  • 12
  • 28
  • I used query with 3 tables Fonctionnaire, PromotionEchelon, Echelon and it gives me result but when i add Mise table it gives me 0 records because there is no Fonctionnaire in Mise – user4340666 Feb 15 '16 at 09:17
  • @user4340666, that's why you have to use LEFT OUTER JOIN and not INNER JOIN – IgorM Feb 15 '16 at 09:28
  • So i use Left OUTER JOIN with Fonctionnaire and Mise? but how can i do with the 2 other tables? – user4340666 Feb 15 '16 at 09:35
  • i tried it and i got this error "Erreur de syntaxe (opérateur absent) dans l'expression « p.CodeFonctionnaire = f.CodeFonctionnaire LEFT JOIN Mise m ON m.CodeFonctionnaire = f.CodeFonctionnaire LEFT JOIN Echelon e ON e.NumEchelon = p.NumEchelon" – user4340666 Feb 15 '16 at 10:02
  • Where is the coma exactly i can't find it – user4340666 Feb 15 '16 at 10:13
  • same error "Erreur de syntaxe (opérateur absent) dans l'expression p.CodeFonctionnaire = f.CodeFonctionnaire LEFT JOIN Mise m ON m.CodeFonctionnaire = f.CodeFonctionnaire LEFT JOIN Echelon e ON e.NumEchelon = p.NumEchelon" – user4340666 Feb 15 '16 at 10:34
  • @user4340666, check data types on both sides, table names and field names – IgorM Feb 15 '16 at 11:09