Bonsoir,

j'ai 4 tables reliées par des clés étrangères et je cherche à obtenir une information d'une de ces tables étant donné une condition (clause WHERE) sur une donnée d'une autre table. La requête que j'ai essayée donne une erreur et je ne sais pas la corriger. Merci de l'aide.

La donnée recherchée est la company d'un user dont on donne l'identifiant :

Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
SELECT CO.CO_company FROM `li_license` LI
JOIN us_user US ON LI.US_ident = US.US_ident
JOIN usl_user_license USL ON USL.US_ident=US.US_ident
JOIN co_company CO ON CO.CO_ident=USL.CO_ident
WHERE `US.US_sesa` = 27384

Code sql : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
CREATE TABLE CO_company(
   CO_ident INT UNSIGNED AUTO_INCREMENT,
   CO_company VARCHAR(50) NOT NULL,
   PRIMARY KEY(CO_ident)
);
 
CREATE TABLE US_user(
   US_ident INT UNSIGNED AUTO_INCREMENT,
   US_sesa INT,
   US_firstname VARCHAR(50),
   US_lastname VARCHAR(50),
   PRIMARY KEY(US_ident),
   UNIQUE(US_sesa)
);
 
CREATE TABLE USL_user_license(
   US_ident INT UNSIGNED,
   US_ident_manager INT UNSIGNED,
   COU_ident INT UNSIGNED,
   LO_ident INT UNSIGNED,
   CO_ident INT UNSIGNED NOT NULL,
   PRIMARY KEY(US_ident),
   FOREIGN KEY(US_ident) REFERENCES US_user(US_ident),
   FOREIGN KEY(US_ident_manager) REFERENCES USL_user_license(US_ident),
   FOREIGN KEY(COU_ident, LO_ident) REFERENCES LO_location(COU_ident, LO_ident),
   FOREIGN KEY(CO_ident) REFERENCES CO_company(CO_ident)
);
 
CREATE TABLE LI_license(
   LI_ident INT UNSIGNED AUTO_INCREMENT,
   LI_activate_date DATE NOT NULL,
   LI_deactivate_date DATE,
   AP_ident INT UNSIGNED NOT NULL,
   US_ident INT UNSIGNED NOT NULL,
   PRIMARY KEY(LI_ident),
   FOREIGN KEY(AP_ident) REFERENCES AP_application(AP_ident),
   FOREIGN KEY(US_ident) REFERENCES USL_user_license(US_ident)
);

Error Code: 1054. Unknown column 'US.US_sesa' in 'where clause'
Je pense inutile de donner la structure des autres tables référencées par des clés étrangères mais non impliquées dans la requête.