Transform rows into columns in MySql - php

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.

Related

JOIN or subquery in SQL to grab data out of various tables needed

I need to pull out some data from various tables using PHP prepared statements and MySQL.
The items I need to plot the data into a graph are:
tblstudent.studentID
tblquestionnaire.questionnaireID
tblstudentAnswer.answer
The database design looks like this with my table joins.
I have attempted to use INNER JOIN's, however I cannot join tblquestionnaire into it as I do not share a key with that table and the student table or with the studentAnswer table.
Any guidance would be much appreciated in how I get those pieces of information out within an SQL query.
You are overthinking it.
to get all student Ids, with their answers and the questionaire ID.
The following query is enough.
SELECT
sta.studenID
,qq.questionnaireID
,sta.answer
FROM
studentAnswer sta
INNER JOIN
questionnaireQuestions qq ON sta.questionnID = qq.questionnID
First of all, you don't need the questionnaire table to get questionnaireID - questionnaireQuestions contains it and it can be directly linked to studentAnswer. Second of all, you don't need the students table to get studentID because studentAnswer contains it. So you can simply follow this logic:
you can get questionnaireID from questionnaireQuestions
you can link questionnaireQuestions with studentAnswer through questionID to get answer and studentID
This bring us to a simple join of two tables:
SELECT sa.studentID, qq.questionnaireID, sa.answer
FROM studentAnswer sa
INNER JOIN questionnaireQuestions qq
ON qq.questionID = sa.questionID
The point is - always look for the shortest route to extract your data. Don't involve any extra tables if it can be avoided. This is where your diagram will come in handy. Looking at it, you can see that questionnaireQuestions is "closer" (one step less to connect) to studentAnswer than questionnaire. Since it contains the data you need, it's logical to use it over questionnaire.
Now let's say you needed questionnaireName along with firstName and lastName of the student. Even though you can't directly join questionnaire to student, you can do it through questionnaireQuestions and studentAnswers. Joins can contain tables you're not selecting from - it's a mechanism of connecting data. Then you'd follow this logic:
you can get questionnaireName from questionnaire
you can link questionnaire to questionnaireQuestions through questionnaireID
you can link questionnaireQuestions to studentAnswer through questionID
you can link studentAnswer to student through studentID
This brings us to a query like this:
SELECT q.questionnaireName, s.firstName, s.lastName
FROM questionnaire q
INNER JOIN questionnaireQuestions qq ON qq.questionnaireID = q.questionnaireID
INNER JOIN studentAnswer sa ON qq.questionID = sa.questionID
INNER JOIN student s ON s.studentID = sa.studentID

How can I link two tables to get data from both - php/phpmyadmin

Hi there sorry I'm not sure how to explain this but I'm creating a website that allows students to loan books out.
I have 2 tables:
Table 1 - BOOKLOAN
BookID
StudentID
DateOut
DateIn
Table 2 - BOOK
BookID
BookTitle
BookAuthor
BookCategory
BookPrice
The website saves all the books that get loaned out to students in the BOOKLOAN table. Students can take out multiple books. How can I get details of a specific students list of books they've taken out? I need all the details from the BOOK table but only the ones where it's listed in the BOOKLOAN table that the student has taken it out?
Does anyone have any idea how I go about this? I have tried doing them as separate queries and as a single one but neither work it just comes up with errors.
you can try whit
Select t0.*,t1.StudentID, t1.DateOut, t1.DateIn
From Book t0
Inner Join Bookloan t1 On t0.BookID = t1.BookID

PHP MS SQL Get Tables Velues even if some table as no value in the other one

I need some help I´m trying to get a list of all clients on my database, it is structured like this:
Table Person has the following fields
PersonId
FName
LName
Age
Gender
And I have another table with the name PersonMoreDetail
This one has the following fields:
PersonId
Adrdress
Nr
Location
Country
Where PersonMoreDetail.PersonId matches with the Person.PersonId.
And I have the following query:
SELECT Person.*
FROM Person INNER JOIN
PersonMoreDetail ON Person.PersonUId = PersonMoreDetail.PersonUId
And it shows only the Persons that have details in the table PersonMoreDetail.PersonId, so if you don't have details you will not appear, I don't know how to fix this, how to show the ones that do not have details on the list.
Solved with
SELECT Person.*
FROM Person LEFT JOIN
PersonMoreDetail ON Person.PersonUId = PersonMoreDetail.PersonUId
Thanks to #CD001 and #mim. that helped me reach the answer!

Getting Data From Another Table

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>

mysql combining data from multiple records in single record

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

Categories