PHP mySQL - Get data from 3 tables from database - php

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.
)

Related

What join to use

I have two tables, one for registered users and one to store votes.
We are logging in with registrants.id and registrants.zipcode. Once they vote their votes are inserted into the votes table, along with their Registration ID.
Im trying to right a select statement that returns a record that will select all the records for Matched ID and Zipcode, but the ID is not in the Votes.voter column. i have tried all kinds of variations of all the joins i can think of. is it something simple i am missing.
SELECT * FROM registrants
LEFT JOIN votes on registrants.id = votes.voter
WHERE registrants.id = 1 AND registrants.zipcode = 46706 and votes.voter <> 1
Perhaps a not exists query:
select * from registrants
where registrants.zipcode = '46706'
and not exists (select 1 from votes where registrants.id = votes.voter)

Multiple PHP subqueries not giving desired result

I have a MySQL table from which I want to extract attendance information(Student Id, course/subject for attendance, date range,whether the student was present or not). I have written the following query:
SELECT
COUNT(a_id),
(
SELECT COUNT(*) FROM attendance
WHERE state = 'present'
AND `dater` BETWEEN '$a' AND '$b'
) AS Count,
stud_id
FROM attendance
WHERE
stud_id =(SELECT id FROM users WHERE NAME = '$stud')
Which is giving me the correct results, but when I change the student,its not giving me the correct count for the days recorded for present. Not mention that I have not yet added the course parameter into the query
The MySQL table is as follows:
I need help for the query to return the desired results(Count the accurate days present for each student, as well as adding the course parameter into the query so that the query will look for attendance records for a specific course, for a specific student, for a specified date range).
Looks like you want to seperate your queries:
Select (select count(*) from <database>.attendance where state = 'present' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as present, (select count(*) from <database>.attendance where state = 'absent' AND (dater between '$a' and '$b') AND name=(SELECT id FROM users WHERE NAME = '$stud')) as absent from <database>.attendance WHERE stud_id =(SELECT id FROM users WHERE NAME = '$stud');
try this :)
Resolved it using JOIN as follows:
SELECT u.id, a.stud_id, a.course_id, count(*) FROM attendance a
JOIN users u ON u.id=a.stud_id
JOIN courses c ON c.c_id=a.course_id
WHERE a.state='present' and dater between '2017-09-01' and '2017-09-14'
GROUP BY a.stud_id, a.course_id;
Thanks for your help.

MySQL query - Using multiple tables

I have 4 tables. (Simplified the 4 tables for the question)
Table 1: Supplier - Id, Type, TypeId, ExpiryDate
Table 2: SupplierType1 - Id, UserId, SupplierName
Table 3: SupplierType2 - Id, UserId, SupplierName
Table 4: User - Id, Name
All Id's are simply auto-increments in their tables.
In Supplier:
'Type' refers to either SupplierType1 or SupplierType2
'TypeId' is the 'Id' from either SupplierType1 or SupplierType2.
In both SupplierType1 and SupplierType2:
'UserId' is the Id from User
I have the 'UserId' (shown as X below). I want to return a list of which has these columns:
Supplier.Id, SupplierType1.RetailName, ExpiryDate
Where (SupplierType1.UserId == 'X' AND Supplier.TypeId == SupplierType1 AND Supplier.ExpiryDate < CurrentDate)
I've only recently starting using mysql and my current solutions seem way too cumbersome. Keen to learn a better way to do this. I would appreciate the help. Thanks in advance!
query
$qry = "
SELECT *
FROM Supplier s
LEFT JOIN SupplierType1 s1 ON s1.id=s.TypeId
LEFT JOIN User u ON u.Id=s1.UserId
WHERE s1.UserId='X' AND s.TypeId='SupplierType1' AND s.ExpiryDate<CURDATE()
";

select datas from firsttable and order by field in secondtable in mysql

I have two tables. First table is je_addchoice, which contains fields like
choiceid
pollid
choicename
choicecreatorid
and the second table is je_uservote and the fields are
userid
pollid
choiceid
What i want to do is,
Display the choice names based on the no of votes in the je_uservote table
$query = select * from je_addchoice where poll_id='$poll_id' //order by (count(choiceid)) from second table
//QUERY FOR DISPLAY CHOICENAMES BASED ON COUNT OF VOTES
How to write the above query
My question is how to access the no of counts in the jeuservote table and display the choicenames based on the result count. Actually the votes for the choicenames in the addchoice table count is stored in the jeuservote table. How can i access the vote count for the choice names
SELECT *, (
SELECT count(*)
FROM je_uservote T2
WHERE T2.pollid=T1.pollID
AND T2.choiceid=T1.choiceID) AS votes
FROM je_addchoice T1
ORDER BY votes

Can't figure out the system for "approve post once 10 users like it"

I have a voting system for articles. Articles are stored in 'stories' table and all votes are stored in 'votes' table. id in 'stories' table is equal to item_name in 'votes' table (therefore each vote is related to article with item_name).
I want to make it so when sum of votes gets to 10 it updates 'showing' field in 'stories' table to value of "1".
Here is a query I use to insert votes into database now (I was thinking to add something to it or create another query to sum vote_values of article user is voting on and see if they are > 10 if yes set showing = 1) :
$q = "INSERT INTO {$this->votes_table} (`vote_value`, `item_name`, `ip`) VALUES({$dir}, '{$story_id}', '{$ip}')";
Here is my database structure:
Stories table
Votes table
Try:
CREATE TEMPORARY TABLE tmp_ids SELECT s.id
FROM stories s
JOIN votes v ON v.item_name = s.id
WHERE s.showing != 1
GROUP BY s.id
HAVING SUM(v.vote_value) >= 10;
UPDATE stories SET showing = 1 WHERE id IN (SELECT id FROM tmp_ids)
// EDIT version on every vote
UPDATE stories s
SET s.showing = IF((SELECT SUM(vote_value) FROM votes WHERE item_name = ?) >= 10, 1, 0)
WHERE s.id = ?

Categories