确定哪些员工在这里最长

时间:2017-08-08 14:29:54

标签: sql

所以我有这张桌子。我怎么知道哪个员工来这里的时间最长?

create table Staff (
    StaffId     char(5) not null,
    StaffName   varchar(20) not null,
    StaffPhone  varchar(13),
    StaffIc     char(14),
    JoinDate    date,
    primary key(StaffId),
    constraint chk_StaffIc check (REGEXP_LIKE(StaffIc,'\d{6}\-\d{2}\-
    \d{4}')),
    constraint chk_StaffPhone check (REGEXP_LIKE(StaffPhone,'^01[0-9]-
    \d{7}|\d{8}$'))
);

2 个答案:

答案 0 :(得分:1)

时间最长,请使用top 1并按JoinDate升序排序。最短的将是下降:

select      top 1
            StaffId,
            StaffName,
            StaffPhone,
            StaffIc,
            JoinDate,
from        Staff
order by    JoinDate asc

答案 1 :(得分:0)

这会告诉你多长时间,也可能是月,年等等。  见DATEDIFF()

select top 1 staffid, staffname, DATEDIFF(day,joindate,getdate()) from Staff
order by joindate 

这使用GETDATE()来获取当天