J'ai trois tables.
emplacements
ID | NAME | TYPE |
1 | add1 | stat |
2 | add2 | coun |
3 | add3 | coun |
4 | add4 | coun |
5 | add5 | stat |
écoles
ID | NAME
1 | sch1
2 | sch2
3 |sch3
emplacement_école
ID |LOCATIONS_ID |SCHOOL_ID
1 | 1 |1
2 | 2 |2
3 | 3 |3
Ici, les emplacements des tables contiennent tous les emplacements de l'application. Les emplacements pour l'école sont appelés par des ID.
quand j'utilise la requête
select locations.name from locations where type="coun";
il affiche les noms de type "coun"
Mais je veux afficher locations.name où seuls les school_locations ont type = "coun"
j'ai essayé de suivre les requêtes, mais aucune ne semble fonctionner
select locations.name
from locations
where type="coun"
inner join school_locations
on locations.id=school_locations.location_id
inner join schools
on school_locations.school.id=schools.id;
et
select locations.name
from locations
inner join school_locations
on locations.id=school_locations.location_id
inner join schools
on school_locations.school.id=schools.id where type="coun";
est-il possible d'utiliser plusieurs jointures internes dans les requêtes, ou existe-t-il une autre manière?
SELECT `locations`.`name`
FROM `locations`
INNER JOIN `school_locations`
ON `locations`.`id` = `school_locations`.`location_id`
INNER JOIN `schools`
ON `school_locations`.`school_id` = `schools_id`
WHERE `type` = 'coun';
la clause WHERE
doit être à la fin de l'instruction
Essaye ça:
SELECT Locations.Name, Schools.Name
FROM Locations
INNER JOIN School_Locations ON School_Locations.Locations_Id = Locations.Id
INNER JOIN Schools ON School.Id = Schools_Locations.School_Id
WHERE Locations.Type = "coun"
Vous pouvez joindre Emplacements à School_Locations puis School_Locations à School. Cela forme un ensemble de tous les emplacements et écoles associés, que vous pouvez ensuite utiliser en utilisant la clause WHERE pour ceux dont l'emplacement est de type "coun".
Essaye ça :
SELECT
(
SELECT
`NAME`
FROM
locations
WHERE
ID = school_locations.LOCATION_ID
) as `NAME`
FROM
school_locations
WHERE
(
SELECT
`TYPE`
FROM
locations
WHERE
ID = school_locations.LOCATION_ID
) = 'coun';