2009-09-03 54 views
0

順序按不工作的MySql順序按不工作MySql上

的代碼如下,

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

+1,這種方式較好。 – 2009-09-03 05:52:56

+0

感謝Buddy +1爲你提供更好的答案 – 2009-09-03 06:58:52

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