0

新手用户。

我有两张桌子:

personnel(personnel_id, employee_id, f_name, l_name, initials, department, role), track(track_id, personnel_id, status, time_in, time_out, location)

“一个人可以被追踪很多次”

为了尝试仅呈现数据集的所需部分,我使用了以下查询:

SELECT 
    personnel.personnel_id, 
    personnel.employee_id, 
    personnel.l_name,  
    personnel.initials, 
    track.status, 
    track.time_in, 
    track.time_out 
FROM
    personnel, track
WHERE
    personnel.personnel_id = track.personnel_id
ORDER BY
    l_name  

这里的问题是它返回track表中外键出现的每个实例,而我需要做的就是显示带有最近插入的轨道数据的人员详细信息。每次出现外键时,它还会多次返回相同的人员数据。我知道问题出在查询中,我已经冒险进入 JOINS,但我仍然处于初学者领域。

4

2 回答 2

0
SELECT
  p.personnel_id, 
  p.employee_id, 
  p.l_name,  
  p.initials, 
  outer_track.status, 
  outer_track.time_in, 
  outer_track.time_out 
FROM Personnel P, Track OuterTrack 
where P.personnel_id = OuterTrack.personnel_id and
OuterTrack.id = 
    (SELECT FIRST t.id from InnerTrack 
     order by InnerTrack.time_in desc 
     group by InnerTrack.personnel_id)
ORDER BY
    Personnel.l_name
于 2012-08-26T01:07:23.683 回答
-1
于 2012-08-26T00:39:29.603 回答