I have two tables TEACHERS
and ABSENCES
they both have column unitid
so if unitid
at TEACHERS
table is 1
I want to insert that number 1
to unitid
in ABSENCES
table:
I think the query should look something like this
<!-- language: lang-sql -->
INSERT INTO ABSENCES (unitid)
select unitid
from TEACHERS
where unitid = teachers id here(please advise what i should put here)
Furthermore, my application is on PHP so maybe i can use if statements or something
Edit:
I just want to assign the value of unitid in teachers to unitid in absences.
The idea is this: Unit id 1 (means teacher teaches maths) so when i put unitid 1 to absences means that the student was absent in maths.
The query that already exists is this:
mysql_query("insert into absences (student_id, date) values ('".$_GET['student_id']."','".date('Ymd H:i:s')."')");
MORE SPECIFICALLY TABLES ARE:
SUBJECTS ABSENCES TEACHERS unitid absence_id username unitname student_id password date unitid unitid
The way you asked the question it would just be:
INSERT INTO ABSENCES (unitid) VALUES (1);
If unitid is 1, you insert 1, it doesn't make sense without a primary key to support it.
If unitid is 1 and you want to insert the teacher id, it would make more sense, but still you don't seem to have any need for the ABSENCES table unless you are trying to insert multiple rows with the same teacher id. In that case you need a composite key with a second field, like date for instance.
INSERT INTO ABSENSES (teacher_id, date)
SELECT teacher_id, CURDATE() FROM teachers WHERE unitid = 1
In this case both teacher_id and date should be the primary key.
I assume that 'unitid' is a primary key in 'Teachers' table and is foreign key in 'Absences' table.
use 'mysql_insert_id()' to fetch the last inserted id from the 'Teachers' table and use the same to insert into the 'Abscences' table the following is the snippet -
mysql_query("INSERT INTO TEACHERS (...) values (...)");
$unitid = mysql_insert_id();
Then use the above $unitid to insert into the 'Abscences' table as follows -
mysql_query("INSERT INTO ABSCENCES (unitid,other_cols) values ('$unitid',other_values)");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.