Click here to Skip to main content
15,894,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone, I am building an attendant system, and I have two tables (students- attend)
So, all the students will be registered at the student table let’s say we only have five students as shown in the photo.
When the student signed in, he/she will be inserted to attend table as shown in the photo and the word present will be written under attend table head.
But I want when I click a button after the class finish to insert the rest of the student that didn’t sign in in this case (Sara and Zain) to attend table with the word absent. How I can do that.


Here is the photo
Thank you

What I have tried:

I succeed when inserting the present student. I tried to do a join between tables but can't do it correctly + I don't know if joining between tables is the perfect solution and if yes I don't know how to ably it correctly.
Posted
Updated 17-Jan-22 10:28am

Try the following query to insert absent students into attend table on a button click:
'Day_date: 2022-01-17 is the control date on which you are performing action. You can pass the parameter to run this query'

SQL Query:
INSERT INTO attend
SELECT Id, Number, fullName, 'absent', '2022-01-17'
FROM student 
WHERE Id NOT IN (SELECT Stu_id FROM attend WHERE Day_date = '2022-01-17')


MySQL Query:

INSERT INTO attend
SELECT Id, Number, fullName, 'absent', '2022-01-17'
FROM student 
WHERE Id NOT IN (SELECT Stu_id FROM attend WHERE Day_date = '2022-01-17')
 
Share this answer
 
Comments
Maciej Los 17-Jan-22 16:22pm    
As far as i understand, you OP is able to insert data into another table...
M Imran Ansari 17-Jan-22 16:29pm    
Yes. As per problem statement "insert the rest of the student that didn’t sign in into attend table"
proud_developer 17-Jan-22 17:32pm    
yes I want the rest of the students that are not at attend table, I will try the query and see its result. Thank you.
proud_developer 17-Jan-22 17:53pm    
I write this query and when I execute it, doesn't insert anything to the attend table:
$q = INSERT INTO attend SELECT id, fullName, number, 'absent', 'curdate()'
FROM student
WHERE id NOT IN (SELECT stu_id FROM attend WHERE day_date = 'curdate()')";

curdate = the current date because I inserted date with current date and system will take the attendant once every day.
M Imran Ansari 18-Jan-22 5:59am    
What is value of curdate()? and check the format of date which already exists inthe attend table.
If i understand you correctly... you want to display all 5 records from attend table and corresponding data from student table.

So, use LEFT JOIN:
SQL
SELECT A.*, B.*
FROM attend AS A
LEFT JOIN student AS B ON A.number = B.number


For further details, please see:
Visual Representation of SQL Joins[^]
SQL Joins[^]
 
Share this answer
 
You do NOT copy names from one table to another. That's duplicating data and it's wasteful.
If you need to change the state of a student in the "attend" table, you put the ID of the student in that table, not the student's name.
 
Share this answer
 
Comments
proud_developer 17-Jan-22 17:31pm    
but I am using a PHP spreadsheet to print out the content of the attend table to xlxs file and I achieve that + I am not very good with MySQL and I am still trying to understand and ably the concept of joining tables together.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900