无法创建连接以查找特定数据

时间:2014-04-22 03:09:54

标签: sql join inner-join

我正在使用Oracle SQL(TeraTerm), 和 我正在尝试加入两个表顾问和PROJECT_CONSULTANT的具体信息,我只需要检索工作超过40小时的员工。这是表格

项目顾问

    PROJECT_ID      CONSULTANT_ID   NUMBER_HOURS
    --------------- --------------- ------------
    94738949        49620928                   6
    45699847        34879223                  57
    45699847        95928792                  44
    45699847        04875034                  59
    19870398        49620928                  32
    30495394        95928792                  57
    30495394        07811473                  50
    62388923        07811473                  82

和顾问

    CONSULTA NAME                             ZIP   START_DT
    -------- -------------------------------- ----- ---------
    CON_TITLE
    -------------------------
    49620928 Tom Jones                        39875 01-SEP-98
    Junior Consultant

    04875034 Jack Johnson                     29087 05-OCT-93
    Manager

    34879223 Lanny Harris                     03944 30-APR-04
    Principal


    CONSULTA NAME                             ZIP   START_DT
    -------- -------------------------------- ----- ---------
    CON_TITLE
    -------------------------
    95928792 Michael Johnson                  02953 22-JUN-02
    Senior Manager

    07811473 Wendy Adams                      29087 05-JUL-05
    Senior Consultant

我提出的代码是

    select Consultant_ID, Name, Zip, and Number_Hours 
    from Consultant
    Inner Join project_consultant
    ON Consultant.Consultant_ID=project_consultant.Consultant_ID
    WHERE project_consultant.number_Hours>40;

我收到错误

    ERROR at line 1:
    ORA-00936: missing expression

我只是想知道如何正确编写连接语句,任何帮助都会很棒,因为我无法知道如何修复此连接语句

3 个答案:

答案 0 :(得分:1)

您未在and条款中使用select

select c.Consultant_ID, c.Name, c.Zip, pc.Number_Hours 
from Consultant c Inner Join
     project_consultant pc
     on c.Consultant_ID = pc.Consultant_ID
where pc.number_Hours > 40;

您还需要在select子句中使用表别名,以明确表Consultant_Id所引用的内容。

编辑:

您可能实际上希望总结员工的工时。如果是这样,您需要聚合:

select c.Consultant_ID, c.Name, c.Zip, sum(pc.Number_Hours)
from Consultant c Inner Join
     project_consultant pc
     on c.Consultant_ID = pc.Consultant_ID
group by c.Consultant_ID, c.Name, c.Zip
having sum(pc.number_Hours) > 40;

答案 1 :(得分:1)

您不能在选择条款中使用

试试这个

    SELECT C.Consultant_ID, C.Name, C.ip, PC.Number_Hours 
    FROM Consultant C
    INNER Join project_consultant PC
    ON C.Consultant_ID=PC.Consultant_ID
    WHERE PC.number_Hours > 40;

答案 2 :(得分:0)

    select c.Consultant_ID, c.Name, c.Zip, p.Number_Hours 
    from Consultant c
    Inner Join project_consultant p
    ON c.Consultant_ID=p.Consultant_ID
    WHERE p.number_Hours>40;