MySQL and PHP fetch particular student who pass the exam - php

MySQL table name: Student
Fields: id,name,subject1,subject2,subject3.
Need to filter out the students who has more than 150 marks in total.

Use this query:
SELECT id, (SUM(subject1)+SUM(subject2)+SUM(subject3)) AS total FROM student GROUP BY id
You can check if(total > 150) in mysql query..

Related

PHP mySQL - Get data from 3 tables from database

I have 3 tables with their columns, I would like to fetch those students who haven't taken the quiz and those who already took the quiz (separate scripts)
Table quiz
Column id, title, created_at, start_on, end_on, class_name, user_faculty
Table studentclass
Column id, class_name, user_faculty, user_student
Table score
Column id, grade, user_student, status, quiz_id
Where as quiz.class_name = studentclass.class_name, quiz.user_faculty = sctudentclass.user_faculty, studentclass.user_student = score.user_student and quiz.id = score.quiz_id
score.status = 'F' //this defines that the user_student already took the quiz
My query is:
SELECT quiz.id, quiz.title, quiz.start_on, quiz.end_on, quiz.class_name,
quiz.user_faculty, studentclass.user_student, score.status
FROM quiz
INNER JOIN studentclass
ON quiz.class_name = studentclass.class_name
AND start_on >= now()
AND end_on <= now()
LEFT JOIN score
ON quiz.id = score.quiz_id
WHERE score.status IS NULL
If the user_student already took the quiz then he is unable to see the quiz anymore because he already has a row in the score table with the user_student, quiz_id and status. But other user_student who haven't taken the quiz can see the quiz.
I hope you get what I want to figure out.
The following query would list all the student who havnt taken the quiz. Let me know if it serves your purpose. I'll update it as per the need.
You can add the joins accordingly if you want the data from other tables.
select * from studentclass Where id NOT IN (
select user_student from score where quiz_id = YOUR ID //if you wnat to filter it for a specific quiz.
)

php mysql view total amount from the database

I want to echo sum of Amound row with student id but I have another column which is call "type" (it should be have only Admission, Installment, Other).
so when I want to echo it should be show the total of the amount column but not included (Other from "Type")
I want to echo that red box total with particulate student id
select sum(Amount) as total_amount
from `table_name`
where `Type` in('Admission', 'Installment', 'Other') group by Student_ID

PHP- select specific row from MySQL database

I Need to select a specific row from a MySQLi database based off of a value in it.
For example I have a Table with a column named "house", and under that column there are maybe 5 rows with the title "house1" and three rows with the title "house2". I only want to select the rows that have "house1" in them.
This is my code
$query = "SELECT * FROM Hockey WHERE house = house1 ORDER BY attendance desc";
I then want it to make a table with only values from a row if the house is Jacksons
right now if I delete the WHERE part from my query it will make a table but it will have rows from both houses (Jacksons and Martlands)
Thanks!
$query = "SELECT * FROM Hockey WHERE house = 'house1' ORDER BY attendance desc";

How can i retrieve data from a table?

I am working in PHP. This is my query:
$sql = "SELECT *
from place as s
where checkDistance($lati1,$longi1,s.lat,s.lon)<$dist";
This place table has three fields: placeId, PlaceName and Address. Now I want to calculate rating of placeId which are the result of above query. To calculate the rating I have another table rating in which there are two fields: placeId and noOfPerson.
Rating will be calculated by (noOfPerson/maximum_no_of_person) for each placeId. How can i implement this?
Your query could do majority of work here, this will select values from place table joined with number of person for each place, ordered by ranking you need, top-bottom:
SELECT s.placeid, s.PlaceName, s.Address, r.noOfPerson
FROM place as s JOIN rating as r ON (s.placeid = r.placeid)
WHERE checkDistance($lati1,$longi1,s.lat,s.lon)
ORDER BY r.noOfPerson / ( SELECT MAX(noOfPerson) FROM rating ) DESC

Mysql select from a table and insert into another

I have a mysql table called jos_users_quizzes with the following columns:
id
quiz_id
user_id
I have a second table called jos_users with this columns
id
name
username
department
the user_id on first table is linked with the id of second table so
quiz_id = id (jos_users)
How can build a query to multiple insert the ids of a selected department into the jos_users_quizzes table... in one click
I am thinking meabe a sub query or a loop, but no sure how.
Thanks in advance!
INSERT jos_users_quizzes (quiz_id, user_id)
SELECT $quizID, id
FROM jos_users
WHERE department = 'selected department'
With INSERT ... SELECT, you can quickly insert many rows into a table from one or many tables. For example
INSERT INTO jos_users_quizzes (quiz_id)
SELECT jos_users.id
FROM jos_users WHERE jos_users.department = 100;

Categories