2009-09-03 2 views
0

하이 주문으로 MySQL을 작동하지 않음으로써 주문,<strong><em>MySQL의</em></strong></p> <p>코드는 다음과 같다 작동하지

select * from School where School.type = 'HighSchool' 
    order by (select locations.name from locations inner join School_locations 
     on locations.id = School_locations.location_id where 
     School_locations.School_id = School.id and locations.location_country = 'US' limit 1) 

출력 모두뿐만 아니라 상승에 대해 동일한 표시되는 이 문제를 해결하는 방법의 내림차순으로

답변

2

하위 쿼리를 수행 할 필요가 없다고 생각합니다.

SELECT s.* 
FROM School s 
    INNER JOIN School_locations sl ON (s.id = sl.School_id) 
    INNER JOIN locations l ON (l.id = sl.location_id) 
WHERE l.location_country = 'US' AND s.type = 'High school' 
ORDER BY l.name 
+0

하나를 사용할 수 있습니다. –

+0

감사합니다. –

0
select school.* from school 
    inner join school_locations on school_locations.schoolid = school.school_id 
    inner join locations on locations.location.id = school_locations.locationid 
where 
    locations.location_country = 'US' and school.type = 'HighSchool' 
order by 
    locations.name 
limit 1 
0

이 방법은 더 나은,이 쿼리

select School.* from School inner join School_locations 
on School_locations.School_id = School.id 
inner join locations 
on locations.id = School_locations.location_id 
where locations.location_country = 'US' and School.type = 'HighSchool' 
order by locations.name limit 1