mysql视图包含运行状态

时间:2013-05-24 15:34:01

标签: mysql sql

我正在为学校设计一个基于网络的时钟,用于登录学生和员工。

我有两张桌子:

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,但我想将其创建为数据库中的视图

1 个答案:

答案 0 :(得分:0)

我认为最好在第一个表中添加一个标志列来捕获类型或条目,即“In”或“Out”。这可以通过某些结果简化您的查询。