I'm not quite sure what you are trying to do with the mdl_assign table - that is for assignment activities, and won't tell you anything about which students / teachers are enroled in the course.
The query I gave you looks in the mdl_user_enrolments table to get a list of users who are enroled in a particular course. It links with the mdl_user table to look up their name. The mdl_context table is required to translate the courseid into a contextid (contextlevel 50 is a course and the instanceid field is the course id).
I've realised that I've made a mistake in my original query - there isn't a 'roleid' field in mdl_user_enrolments. To see if the user has a role, as well as being enrolled in a course (it is possible to be enroled in a course, without having a role, although the usefulness of this is debatable), you would need to link with the mdl_role_assignments table as well (you may want to have a look in lib/accesslib.php at the 'get_user_roles_in_course' function to help with this).