I have 4 tables:
Student(id(PK),name,subject_enrolled(FK)
Teacher(id(PK),name,subject_teaches(FK)
Subject(subject_code(PK),subject_name)
Combined(FIELDS not yet known)
By using MYSQL triggers, I want to insert data to the student table, it will automatically get the names of the teacher table and place it to the Combined table based on the subject enrolled and the subject teaches. What is the best approach to realize that ?
you can get it USING following query you don't need separate table and triggers
SELECT ST.name as Student_name,T.name as Teacher_name ,S.subject as Subject ,S.subject_code as Subject_code from Student ST
inner join Subject S on S.subject_code=ST.subject_enrolled
inner join Teacher T on T.subject_teaches=S.subject_code
where ST.id=<your student id>
Related
I have two table one is accounts table and account group table. currently I am using the auto increment id as foreign key in the accounts table but my question is I have multiple tables where it will join with accounts table and I need to retrieve account group name in a view where there is 20 union all queries which it join to the accounts table.
SELECT acc.name,accgrp.groupName,bill.supplier,bill.amount
FROM bills bill
INNER JOIN accounts acc ON acc.accId=bills.accId
INNER JOIN accountGroup accgrp ON accgrp.groupId=acc.groupId
UNION ALL
.....
What is the correct way to save the data in accounts table saving id is the best approach or saving the group name is best approach. If i used id as foreign key and join in 20 union all is it make my query slower? or saving group name in my accounts table and retrieve it without join make my query faster?
i know this question has been asked on this platform but I couldn't get enough details to help me with my problem.
I have three tables I am selecting from: student, subjects and exam table.
student table:
Results Table
And the Subjects table:
If you take a look at the first table(results table) you will see the subjects codes. What I am trying to achieve is like the diagram below:
Here is my result table after I selected those results from database for TS2018007.
My query for image four:
SELECT distinct s.regiNo, s.firstName as fname, s.middleName as mname, s.lastName as lname,
s.gender, s.class_group, c.subjects,e.First_CA, e.Second_CA, e.examid, e.scored,
e.internaltype, e.Class, e.Year
FROM student s inner join exam e on s.regiNo = e.Roll_Number
inner join subjects c on e.sub_id = c.subect_code
group by s.regiNo, s.firstName, s.middleName, s.lastName, s.gender, s.class_group,
c.subjects, e.First_CA, e.Second_CA, e.examid, e.scored, e.internaltype,
e.Class, e.Year
How can I create an html table with dynamic rows and columns where row data relies on column heading everytime a new subject score is added to the exam table? I don't want hard code.
Thanks in advance for your help.
I've been following the PHP Authentication Tutorial en managed to get it working.
Now I want to add a function to my navbar. I will try to explain and hopefully someone has the answer.
I have the 2 user tables, users and user_permissions.
In the user table there are additional columns named district_id, district, gemeente_id and gemeente. I also have 2 extra tables in the database called districten and gemeenten.
The districten table has 2 colums. id and district which holds the unique ID and the name. The gemeente table has at least these 3 columns id, district_id and gemeente.
Here is what I would like to happen:
When the user logs in the query will give a result in an <ul> in the navbar where it will show all values (column gemeente) from the gemeenten table where the user has been assigned to.
Example:
User ZZZ is assigned to district A.
This district holds 4 gemeenten : City 1, City 2, City 3 and City 4
When the user logs in, he should
only see the cities from the District A.
I don't know how to get this working in Slim2 Framework and integrated in the PHP Authentication code. So any help is much appreciated.
First you have to normalize your table and get rid of redundant information since it's hard to maintain it in relational database(I assume here you're using relational database like MySQL).
So basically each user is assigned to gemeente, each gemeente assigned to district and district in his turn has cities(gemeenten). In user table you only need the gemeente_id. The schema of districten and gemeente seems fine.
There are two way how to retrieve needed data: by running 2 queries(1 for fetch user's district; 2nd for fetch district's cities) and run everything in one query. Which way to choose is up to you.
1st approach:
SELECT d.id FROM district AS d LEFT JOIN gemeente AS g ON g.district_id=d.id LEFT JOIN user AS u ON u.gemeente_id=g.id WHERE u.id=<user_id>
SELECT g.id, g.gemeenten from gemeente as g where g.district_id = <res_of_prev_query>
2nd approach:
SELECT
g.id, g.gemeenten
FROM gemeente AS g
LEFT JOIN
districten AS d
ON g.district_id=d.id
INNER JOIN (
SELECT DISTINCT district_id, u.id AS user_id
FROM gemeente as gg
INNER JOIN user AS u
ON u.gemeente_id=gg.id) AS ud
ON ud.district_id=g.district_id
WHERE ud.user_id=<user_id>
You need transform this query to your ORM/ActiveRecord, run it and display the ul list
UPD: I've updated my answer. Thanks #DarkBee for pointing me to my mistake
A subset of my database contains 3 tables
users
user_details
tasks
The main column of the user table is:
id {PK}
The user details table contains information about the users (only some users have these details, students). Thus the main columns of the users details table are:
user_id{PK,FK} | supervisor_id {FK}
The supervisor_id is an id in the users table. Each student has one supervisor.
Lastly, there is the tasks table where only students create tasks and the main columns of the tasks table are:
task_id{PK} | user_id{FK}
The problem I am having is getting a proper query for, if a supervisor wants to see all his students tasks. I know you can query all the students in the user_details table who have the supervisor's id. Then create another query where you select all the tasks whose user_id matches that of the first query performed.
This does not seem like a very efficient was to go about achieving this result. Are there better alternatives?
select ud.supervisor_id, ud.user_id, t.task_id
from user_details ud, users u
where ud.user_id = t.user_id
What you are looking for is a join. Instead of writing two separate queries to get the information, a join will allow you to connect the tables that you have in one query and get the information you need much faster.
Select *
From user_details ud
join tasks t
on ud.user_id = t.user_id
Where ud.supervisor_id = ?
The join essentially allows you to create one big table out of all of the columns of the tables you are using. The on keyword tells sql which values go together, so that you know all of the tasks belong to the student whose id matches the id that the supervisor has. Then, you can select whatever columns you like out of either table (as well as a lot of other fancy things).
I am working on a school database system based on php mysql. the basic structure is as below:
Table Class-Details of all classes.
Table Student-Details of all students
Table Semester-Details of all Semesters
Table class–Semester. This table solves many to many relation, primary key- IDs of both class and semester.
Table Subject -Details of all Subjects
Table class–Subject. This table solves many to many relation, primary key- IDs of both class and semester.
Table marks- consists of student ID, Subject ID, Semester ID, Marks Achieved.Foreign Key ClassID, SemesterID, SubjectID
I am trying to get a table in below mentioned format:
ENGINE = InnoDB
AUTO_INCREMENT = 53
DEFAULT CHARACTER SET = utf8;
So far I could achieve individual marks of subjects in every record
my query:
SELECT
m.MarkID, m.studentID,
sm.ExamName, sb.Subject,
sm.MaxMarks, m.MarksRecvd
FROM marks AS m
RIGHT JOIN student AS st
ON m.studentID=st.studentID
RIGHT JOIN semester AS sm
ON m.ExamID=sm.ExamID
RIGHT JOIN subject AS sb
ON m.SubjectID=sb.SubjectID
WHERE m.studentID = $studentID
Please help me on this... I am getting a record for every mark recvd like below.
However I want to group all semester marks in one row like below:
Friends pls let me know if my query is clear