I have various tables like
Student primary id , students name, course Papers paper id, papername, course, semester, type StudentOptions primary id, studentid (foreign key - reference student id) and paperid (foreign key - references paper id) StudentsTerm studentid (foreign key- references student id) and student semester
Now the kind of result i want is,
I want to choose a course then the term, which will give me the number of papers/subject it has with their types (Mandatory/Optional) and with that i want to have the count of number of students studying those papers from all these tables.
I don’t wanna create any view or stuff, Just a normal select query will do.
The query i am running is :
SELECT p_name,
p_id,
type,
Count(sps.studentid) AS counts
FROM students,
str,
papers
LEFT JOIN sps
ON sps.paperid = papers.p_id
WHERE sps.studentid = students.studentid
AND students.studentid = str.studentid
AND sps.studentid = str.studentid
AND str.semesterid = p_semid
AND str.sessionid = 12
AND students.course = c_id
AND c_id = 6
AND p_semid = 1
GROUP BY p_id
As better practice, if you are going to be using explicit
JOINsyntax, then don’t use implicit syntax. In the query you posted above, you select frompapers, but you don’t use it anywhere. Also, your column names are slightly ambiguous. If it’s easier, alias each table using a single letter, or explicitly prefix the column names. If you’re using an aggregate withGROUP BY, you cannot select columns which are not in the group.Let’s assume this is your ER diagram:
Let’s first join all the tables:
Now you wish to find the number of students studying papers from a specific course and type:
Because your attributes are ambigiuous, it is hard to tell what tables they are coming from. If you can set up the structure and sample data on SQL Fiddle, we could help you better.