日期:2014-05-16  浏览次数:20680 次

联合查询问题
SELECT   exam_sort.ID,exam_sort.Title,exam_station_sort.Must,exam_time.Locaton,exam_time.StartTime,exam_time.EndTime,exam_time.ExamDate,count(DISTINCT   exam_sort.Title)   FROM   exam_sort
              LEFT   JOIN   exam_station_sort   ON  
              exam_station_sort.Sort   =   exam_sort.ID
              LEFT   JOIN   exam_time   ON   exam_time.SID   =   exam_station_sort.StationNum
              LEFT   JOIN   exam_student   ON  
              exam_student.SID   =   exam_station_sort.StationNum
              WHERE   exam_station_sort.StationNum   = ' ".$stationNum. " '
              AND   exam_time.Sort   =   exam_station_sort.Sort
              AND   exam_station_sort.Must   =   0
              AND   exam_sort.ID   IN   ( ".$soID[ "SortE "]. ")
              AND   exam_student.UserName   =   ' ".$userName. " '
              GROUP   BY   exam_sort.Title
              ORDER   BY   exam_sort.ID

SELECT   Sort,Title,Results   FROM   exam_results
                WHERE   SID   =   ' ".$stationNum. " '
                AND   UserName   =   ' ".$userName. " '
                AND   Sort   IN   ( ".$soID[ "SortE "]. ")
                GROUP   BY   Title
                ORDER   BY   Sort  

以上两条   sql   语句中   exam_sort.Title与   Title   是一样的,   exam_sort.ID   与   Sort   也是一样的,但是经常会出现   顺序不一样的查询结果,请问为什么?

------解决方案--------------------
GROUP BY exam_sort.Title
ORDER BY exam_sort.ID
用group by title 后 那个order by id 还有用吗?