i apologize for the bad topic title as i am kinda at lost of what should i do here.
First of all, here are my database table designs and i would like to receive some sort of feedback as well before proceeding:
I am trying to make an enrollment web application.
Subjects
subject
[id] [subj] [professor] [cstart] [cend] [days] [count] [units]
1 comalgo carl 10:00am 12:30pm M-W 40 3.0
2 compasm carl 01:00pm 02:30pm T-TH 40 3.0
3 compro miguel 04:30pm 06:30pm M-W 35 3.0
4 pro5 fua 03:30pm 05:30pm T-TH 30 3.0
5 pro5 fua 06:30pm 08:30pm F 10 3.0
Students
students
[id] [fname] [lname] [bday] [unitsleft] [unitstaken] [major]
1 carlos doe 11/20/1990 100 0 BS-COMPUTER SCIENCE
2 miguel doe 08/15/1992 100 0 BS-ECONOMY
3 carl doe 12/12/1991 100 0 BS-PSYCHOLOGY
4 test doe 02/12/1992 100 0 BS-LITERATURE
What i plan on doing is that, the subjects will be presented through a series of list and the student will pick multiple subjects and enroll. The total enrolled units should not be greater than 20 and less than 11. I already implemented that part and i am now working on storing the selected subjects.
Here is my proposed solution and i would like to receive some feedbacks about it and what operation should i use.
I will have a centralized table for all of enrolled subjects
Enrolled subjects
selected subject
[id] [subjid] [studentid] [status]
1 1 1 P //comalgo enrolled by carlos, P is for pending grade
2 1 2 P //comalgo enrolled by miguel P is for pending grade
3 2 1 P //compasm enrolled by carlos P is for pending grade
What i plan to happen is that: i have a profile page for the students and they will be able to view/edit their currently enrolled subjects specifically for themselves(based from ID)
I want them to be presented with the following table headers which is based from the enrolled subjects:
[subj] [professor] [cstart] [cend] [days]
Here are the brief summary of my questions:
1.) is my table alright? or it's a bad design?
2.) What kind of method should i use? i am trying to research about it(joins) but i am somehow confused and in need of clarification. I want to present the users with their selected subjects.
edit: i think i can do something like.. select where id = x from the enrolled subjects then get the subject id but i am not sure if that will be efficient.
Your tables look good (i.e. third normal form). An INNER join will do
SELECT
B.subj, B.professor, B.cstart, B.cend, B.days
FROM
selectedsubjects A
INNER JOIN subjects B ON A.subjid = B.id
WHERE
A.studentid = 1
p.s. This looks like an assignment for school or something out of a book...?
It could be:
var query = context.EnrolledSubjects
.Include("Subject")
.Where(i => i.StudentId == 1)
.Select(i => new {
i.Subject.Subj,
i.Subject.Professor,
i.Subject.CsStart,
i.Subject.Send,
i.Subject.Days,
i.Status
};
Related
I'm having some trouble working out how to write a query. I want to be able to make a list of all teams, and the students within the team that are in a certain subject.
Here are the sorts of tables I have.
Team (teamID, teamName, subjectID)
Student(studentID, Student Name)
AssignTeam(AssignID, studentID, teamID)
Subject(subjectID, subjectName)
This is what I would like the output to look like.
Team 1 - Team Name
Student 1 ID - Student 1 Name
Student 2 ID - Student 2 Name
Student 3 ID - Student 3 Name
Student 4 ID - Student 4 Name
Team 2 - Team Name
Student 1 ID - Student 1 Name
Student 2 ID - Student 2 Name
Student 3 ID - Student 3 Name
Student 4 ID - Student 4 Name
Student 5 ID - Student 5 Name
Team 3 - Team Name
Student 1 ID - Student 1 Name
Student 2 ID - Student 2 Name
Student 3 ID - Student 3 Name
I'm struggling to work out how to format it in such a way to include the breaks between the groups themselves. I only really know how to make a list that looks like this
Group StuID StuName
-- ---- ----
1 1 Mike
1 2 Stacey
1 3 Jenny
2 4 Rick
2 5 Sam
3 6 Larry
3 4 Anita
I want to build the list using mySQL but it will ultimately be outputting via PHP. I was hoping to create a stored procedure which I can then call and pass the subjectID into which will then create the list.
I haven't quite worked out if the one procedure would create the list and convert to string for output or if that should be two separate queries.
Any suggestions would be much appreciated.
Thanks
This is not a mysql solution but would it not be simple enough to read the rows from mysql in PHP, and when the Group changes output the header line for that group?
See the snippet below. You'll have to make outputRow() and outputGroupHeader() generate the output in the format you need.
<?php
$lastgroup = "";
while ($row = mysqli_fetch_assoc($query))
{
if ($row['group'] != $lastgroup) {
outputGroupHeader($row['group']);
$lastgroup = $row['group'];
}
outputRow($row);
}
I have two tables Subjects And Faculty.
Subjects Table
subjects_id subjects_name
1 Maths
2 Science
3 Geography
4 History
5 English
Faculty table
fact_id faculty_name subjects_id
1 Roy 1,2,3
2 James 4,5
Now what i want is to display subject which are taught by Roy in a drop-down(html5).I am confused as in how will i achieve it as the subjects_id are comma separated. I know i will have to use joins but not sure how to actually use it.
Please do help me.
Try this :
select * from subjects
where FIND_IN_SET(subjects_id,(select subjects_id from faculty where fact_id = 1)) <> 0
Subjects for Roy (id = 1)
I tried to create a VIEW that merge some tables in order to have the "hottest" teacher in a educational platform.
First, I have a table with the users (some teachers, some students),
then in other I have the lessons created by the teachers,
finally, other one that has the relation between students and lesson.
when I use this SQL sentence
CREATE OR REPLACE VIEW `skn_teachers` AS
select
`u`.`id_skn_users` AS `id_skn_users`,
`u`.`firstName` AS `firstName`,
`u`.`lastName` AS `lastName`,
COUNT(`ls`.`createdBy`) AS `countLessons`
from (`skn_users` AS `u`, `skn_rolesxusers` AS `rxu`, `skn_roles` AS `ro`, `skn_approved_lessons` AS `ls`)
where ((`rxu`.`id_skn_users` = `u`.`id_skn_users`) and (`rxu`.`id_skn_roles` = `ro`.`id_skn_roles`) and (`ro`.`name` = 'teacher') and (`ls`.`createdBy` = `rxu`.`id_skn_users`))
group by `u`.`id_skn_users`
the row countLessons show me the number of lessons per teacher eg.
id | firstName | lastName | countLessons
1 Pepito Perez 1
2 Julián Figueroa 7
3 Daniel Aguirre 2
which is correctly the number of lessons per teacher.
but I need the number of students that have the lessons created by each teacher (all of them, sum of all students in all lessons of THAT teacher), countStudentsByTeacher, in one of my attempts, get this SQl and it was a surprise when I got the number of students by teacher but I don't understand clearly what I did.
new SQL sentence:
CREATE OR REPLACE VIEW `skn_teachers` AS
select
`u`.`id_skn_users` AS `id_skn_users`,
`u`.`firstName` AS `firstName`,
`u`.`lastName` AS `lastName`,
COUNT(`ls`.`createdBy`) AS `countStudents`
from (`skn_users` AS `u`, `skn_rolesxusers` AS `rxu`, `skn_roles` AS `ro`, `skn_approved_lessons` AS `ls`, `skn_lessonsxusers` AS `lxu`)
where ((`rxu`.`id_skn_users` = `u`.`id_skn_users`) and (`rxu`.`id_skn_roles` = `ro`.`id_skn_roles`) and (`ro`.`name` = 'teacher') and (`ls`.`createdBy` = `rxu`.`id_skn_users`) and (`ls`.`id_skn_lessons` = `lxu`.`id_skn_lessons`))
group by `u`.`id_skn_users`
//
id | firstName | lastName | countLessons
1 Pepito Perez 10
2 Julián Figueroa 15
3 Daniel Aguirre 8
here, column countLessons shows really the number of students into all lessons created by each teacher, exactly what I wanted but I want to know why this works.
Thanks in advance!
In your 2nd query, you added at join to the table skn_lessonsxusers. This causes the 2nd query to return additional rows per each student that was assigned the lesson. The first query only returned the lessons that were created by each teacher.
Example:
Looking at the teacher "Daniel Aguirre", the underlying non-aggregated data for the first query might look like this:
id | firstName | lastName | Lesson
3 Daniel Aguirre Math 101
3 Daniel Aguirre CS 104
So there are 2 lessons that he teaches.
Now if you add in the students for each lesson, i.e. the 2nd query, then the data might look like this.
id | firstName | lastName | Lesson | Student
3 Daniel Aguirre Math 101 John
3 Daniel Aguirre Math 101 Bob
3 Daniel Aguirre Math 101 Sara
3 Daniel Aguirre Math 101 Mary
3 Daniel Aguirre CS 104 John
3 Daniel Aguirre CS 104 Bob
3 Daniel Aguirre CS 104 Dan
3 Daniel Aguirre CS 104 Sally
So now when you aggregate this 2nd set of data, it will show a count of 8 because there are 8 student/lesson combination for Daniel Aguirre.
Now assuming that John and Bob in the above 2nd data set are the same student, there are not really 8 different students taking lessons from Daniel Aguirre, there are 6 (John, Bob, Sara, Mary, Dan, and Sally). If you wanted to show 6 in this example you would use:
COUNT(DISTINCT `lxu`.`student_id`) AS `countStudents`
where student_id is the column that represents a unique student in the skn_lessonsxusers table. NOTE: your column is probably a different name than "student_id", but I don't know your table structure for this example so I just used "student_id".
It works because you multiply each lesson by each student in this lesson
join... 'skn_approved_lessons' AS 'ls'
where... and ('ls'.'createdBy' = 'rxu'.'id_skn_users')
I have 2 tables in Access with these fields
Student:
ID(PK) Name Family Tel
Lesson:
ID StudentRef(FK(Student)) Name Score
Imagine we have these records
Student :
1 Tom Allen 09370045230
2 Jim leman 09378031380
Lesson:
1 1 Math 18
2 1 Geography 20
3 2 Economic 15
4 2 Math 12
How can I write a query that result will be this (2 fields)?
Tom Math : 18 , Geography 20
Jim Economic :15 , Math :12
SELECT s.Name, l.Name, l.Score
INNER JOIN tbl_lessons as l ON s.student_id = l.student_id
FROM tbl_students as s
That won't give you your formatting, but it'll get you the data.
The most tricky part of your problem is how to aggregate strings in your sub-query. MS Access does not have any aggregation function that is applicable to strings (except for Count()) and there is no way to define your own function. This means you can't just get the desired "subject:score , subject:score" concanetation. As long as you can go without you can easily take the solution provided in the answer by Corith Malin.
I have a database two tables and a linking table that I need a JOIN query for:
Here are my Tables:
family (userid (int), loginName, etc)
member (memberid (int), loginName(this links member to a family),name, etc)
Linking Table:
user2member (userid,memberid)...would both be foreign keys?
I want to do two things:
1) Be able to have a family.loginName(12,Johnson) subscribe to another family.loginName (43,Smith) and record that into the linking table.
That would look like this:
12,43
2) When I do a query for all the member.name that are in the Johnson Family, I'll get all the Johnsons & all the Smiths.
If Johnson = Ted, Sue & Patty
IF Smith =Joe, Sue & Bob
my query results would be Johnson now = Ted,Sue,Patty,Joe,Sue,Bob
I asked this question a few days ago without good table names and I ended up confusing myself and the nice guy Ollie Jones who posted an answer similar to this for the query:
SELECT member.name
FROM family
JOIN user2member on family.userid = member.memberid
JOIN member on user2member.name = member.name
WHERE family.userid = '30'
ORDER BY member.name
I had to change Ollie's answer to match my tables but I'm getting a limit error 0,30 on line 5.
This is my first time doing JOINS and I have no idea if this is correct.
Thanks,
Here's the link to my first question: mySQL table linking , group linked to other members lists, the displaying all members
I am not sure, if the tables you suggested would solve your problem. If I understand your question correct, there are two relationships:
a relationship for all family members (Johnson with Ted, Sue, Patty, Smith with Joe, Sue, Bob)
a relationship for subscriptions (a family can subscribe to another family)
I would suggest following tables:
family (f_id, f_loginName, etc.)
member (m_id, m_f_id, m_name) (foreign key to family, many-to-one relationship)
subscription (s_f_id,s_to_f_id) (linking is based on both family keys)
This would result in following contents:
family:
f_id f_loginName
12 Johnson
43 Smith
member:
m_id m_f_id m_name
1 12 Ted
2 12 Sue
3 12 Patty
4 43 Joe
5 43 Sue
6 43 Bob
subscription
s_f_id s_to_f_id
12 43
Now, to get all possible members for a specific family and it's subscriptions, I would use following SQL query. It has a simple join for family and it's family members. In the WHERE clause, the family Johnson is fetched (f_id = 12) and to get all family members from the subscriptions, it's easier to use a subquery.
SELECT f_loginName, m_name
FROM family
INNER JOIN member ON m_f_id = f_id
WHERE f_id = 12
OR f_id IN (SELECT s_to_f_id FROM subscription WHERE s_f_id = 12)
ORDER BY f_loginName, m_name;