2017-08-14 58 views
0

我輸出的問題是這樣的: -如何訂購SQL中的兩列?

FIRSTNAME COURSENAME 
---------- -------------------- 
Anand  C++ 
Bala  C++ 
Dileep  Linux and C 
Gowri  Java 
Gowri  Linux and C 
Gowri  C# 
John  C++ 
John  Oracle 
Prem  Linux and C 
Priya  Java 
Priya  Oracle 
Priya  C# 
Rahul  Oracle 

但預期輸出是: -

FIRSTNAME COURSENAME 
---------- -------------------- 
Anand  C++ 
Bala  C++ 
Dileep  Linux and C 
Gowri  C# 
Gowri  Java 
Gowri  Linux and C 
John  C++ 
John  Oracle 
Prem  Linux and C 
Priya  C# 
Priya  Java 
Priya  Oracle 
Rahul  Oracle 

我的代碼:

select firstname, coursename 
from course 
inner join 
    (select student.firstname as firstname, registration.courseid 
    from student 
    inner join registration on student.studid = registration.studid 
    group by student.firstname, registration.courseid) q1 on q1.courseid = course.courseid 
order by firstname asc; 

如何安排在上升兩列訂單保持另一個固定? (也許我沒聲音在解釋這個好)

+0

你想通過列名字訂購然後coursename? –

+1

可能重複的[SQL多列排序](https://stackoverflow.com/questions/2051162/sql-multiple-column-ordering) –

回答

3
order by firstname, coursename 

或者

order by firstname asc, coursename asc 
+0

我試過,即使....我得到了不同的輸出 –

+0

這是預期的結果集。你如何結束Gowri&Priya coursename訂購? –

+1

Arun的答案是正確的,因爲按字母順序J在L之前 – Khalil