我正在为一所学校设计一个基于网络的时钟,用于登录学生和教职员工。
我有两张桌子:
student_time_clock: student_id (foreign key to student), time_stamp, by_adult_id (foreign key to adult)
staff_time_clock: staff_id (foreign key to staff), time_stamp
我得到的要求之一是观察者能够在一个视图中查看记录,如下所示:
+----------+------------+-------------------------+---------------------+
| In / Out | Time Stamp | Person Logged in or out | Logged in or out by |
+----------+------------+-------------------------+---------------------+
除了第一列之外,我可以用工会做所有事情。我不知道如何获得第一列。这是我用于联合的查询:
SELECT stc.entry AS "Time Stamp",
Concat(s.lastname, ",", s.firstname) AS "Punched",
Concat(a.lastname, ",", a.firstname) AS "By"
FROM student_time_clock stc,
student s,
adult a
WHERE stc.student_id = s.id
AND stc.by_adult_id = a.id
UNION
SELECT atc.entry AS "at",
Concat(a.lastname, ",", a.firstname) AS "Staff",
Concat(a.lastname, ",", a.firstname) AS "By"
FROM staff_time_clock atc,
staff s,
adult a
WHERE atc.staff_id = s.id
ORDER BY "time stamp" DESC;
我尝试过使用 CASE,例如:
CASE COUNT( entry ) % 2 WHEN 1 THEN "In" WHEN 0 THEN "Out"
当这种情况存在时,尽管我在结果中只得到一行。
有什么建议么?前端是 PHP,但我想在数据库中创建它作为视图