0

你好我必须写一个查询,我必须显示:

  • 经理编号
  • 经理的全名(第一列、中间列和最后一列的组合)
  • 员工编号
  • 员工全名(又是名字、中间名和姓氏)

他们都必须为一位特定的经理戴夫。如果有帮助的话,Dave 的员工 ID 是 100。

到目前为止我有

SELECT 
    EMP_MGR as "Manager's Number", 
    EMP_NUM as "Employee's Number", 
    EMP_FNAME || ' ' || EMP_INITIAL || '. ' || EMP_LNAME AS "Employee's Full Name", 

FROM EMP
    WHERE EMP_MGR = 108

我知道我必须在某处使用连接,但我对如何做到这一点非常迷茫。

4

3 回答 3

0
SELECT 
    A.EMP_MGR as "Manager's Number", 
    B.EMP_NUM as "Employee's Number", 
    A.EMP_FNAME || ' ' || A.EMP_INITIAL || '. ' || A.EMP_LNAME AS "Manager's Full Name",
    B.EMP_FNAME || ' ' || B.EMP_INITIAL || '. ' || B.EMP_LNAME AS "Employee's Full Name", 

FROM EMP A
INNER JOIN EMP B ON A.EMP_NUM = B.EMP_MGR AND A.EMP_NUM = 108
于 2012-11-19T14:26:50.593 回答
0

你可以这样做

select
    M.EMP_NUM as "Manager's Number", 
    M.EMP_FNAME + ' ' + M.EMP_INITIAL + '. ' + M.EMP_LNAME AS "Manager's Full Name", 
    E.EMP_NUM as "Employee's Number", 
    E.EMP_FNAME + ' ' + E.EMP_INITIAL + '. ' + E.EMP_LNAME AS "Employee's Full Name"
from EMP as M
    inner join EMP as E on E.EMP_MGR = M.EMP_NUM 
where M.EMP_NUM = 100
于 2012-11-19T14:24:01.210 回答
0

尝试这个:

select
    M.EMP_NUM as "Manager's Number", 
    M.EMP_FNAME + ' ' + M.EMP_INITIAL + '. ' + M.EMP_LNAME AS "Manager's Full Name", 
    E.EMP_NUM as "Employee's Number", 
    E.EMP_FNAME + ' ' + E.EMP_INITIAL + '. ' + E.EMP_LNAME AS "Employee's Full Name"
from EMP as M
    inner join EMP as E on E.EMP_MGR = M.EMP_NUM 
where M.EMP_NUM = '100'

唯一的区别是我将 100 放在单引号中。我的猜测是名为“EMP_NUM”的字段存储为字符串而不是数字。如果是这样,请向数据库设计人员抱怨选择了一个非常糟糕的名称。不会有帮助的。. . 但他们可能知道。

于 2012-11-19T14:45:01.783 回答