小编典典

在MySQL中使用where和内部联接

mysql

我有三张桌子。

地点

ID   | NAME | TYPE |
1    | add1 | stat |
2    | add2 | coun | 
3    | add3 | coun |
4    | add4 | coun | 
5    | add5 | stat |

学校

 ID | NAME  
 1  | sch1     
 2  | sch2
 3  |sch3

school_locations

 ID |LOCATIONS_ID |SCHOOL_ID
 1  | 1           |1
 2  | 2           |2
 3  | 3           |3

此处的表位置包含应用程序的所有位置。学校的位置由ID调用。

当我使用查询

select locations.name from locations where type="coun";

它显示“ coun”类型的名称

但我想显示location.name,其中只有school_locations的类型为“ coun”

我尝试了以下查询,但似乎都没有用

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;

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";

是否可以在查询中使用多个内部联接,还是有另一种方法?


阅读 305

收藏
2020-05-17

共1个答案

小编典典

    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';

WHERE子句必须在语句的末尾

2020-05-17