Reputation: 1983
I am now having a problem sorting the data in my PHP page where the data displayed is combined of two tables as the both tables are linked by a foreign key in one of the table.
two tables are as below
Table name: Students
stu_id
stu_name
.
.
.
stu_course_id
Table name: courses
course_id
course_name
Wen displaying the data it is displayed in following format:
Student id | Student name | student course
----1 --------------john-------------engineering
----2--------------dave---------------business
I am able to sort the data by name which is pretty easy but I am having difficulty sorting the data by course name. Is this possible as the course name is not in the same table as student?
Upvotes: 1
Views: 96
Reputation: 411
Yes, use the ORDER BY clause.
SELECT * FROM courses ORDER BY course_name
Upvotes: 1
Reputation: 5022
Of course. Simply refer to the column name without ambiguity, i.e.:
ORDER BY courses.course_name
Show me your query and I'll make that work.
Upvotes: 1
Reputation: 27247
select s.stu_id, s.stu_name, c.course_name
from students s
inner join courses c on s.stu_course_id = c.course_id
order by c.course_name asc
Upvotes: 4