在以下查询中需要帮助

时间:2019-03-03 08:25:55

标签: mysql sql

列出员工及其经理的详细信息,其中员工薪水大于经理薪金

我的表格如下:

//employee table
`Employee` (
`Ename` VARCHAR( 25 ) NOT NULL (primary key) ,
`Street` VARCHAR( 40 ) NOT NULL ,
`City` VARCHAR( 20 ) NOT NULL ,
`joining_date` DATE NOT NULL ,
`gender` VARCHAR( 7 ) NOT NULL ,
PRIMARY KEY ( `Ename` ) 
) ENGINE = MYISAM ;


//company table
 `Company` (
`Cname` VARCHAR( 25 ) NOT NULL ,
`city` VARCHAR( 20 ) NOT NULL ,
PRIMARY KEY ( `Cname` ) 
) ENGINE = MYISAM ;



//works table
`Works` (
`ename` VARCHAR( 25 ) NOT NULL ,
`cname` VARCHAR( 25 ) NOT NULL (foreign key),
`salary` INT( 50 ) NOT NULL ,
PRIMARY KEY ( `ename` ) 
) ENGINE = MYISAM ;



//manages table
 `Manages` (
`empname` VARCHAR( 25 ) NOT NULL ,
`manager_name` VARCHAR( 25 ) NOT NULL 
) ENGINE = MYISAM ;
here ename and manager name is composite key


//dependent table
employee_dependent(
employee_name VARCHAR(15) NOT NULL(foreign key),
dependent_name VARCHAR(25) NOT NULL,
relation VARCHAR(25) NOT NULL,
    gen VARCHAR(25) NOT NULL,
    PRIMARY KEY(dependent_name)
);

1 个答案:

答案 0 :(得分:0)

您可以像下面这样编写查询,以获取员工薪水高于经理薪水的地方。

您需要加入员工并工作两次,一次给员工,一次给经理。

SELECT e.*,m.Ename as manager,  
FROM Employee e
    INNER JOIN Manages rel on e.ename = rel.empname
    INNER JOIN Employee m on m.ename = rel.manager_name
    INNER JOIN Works we on we.ename=e.empname
    INNER JOIN  Works wm on wm.ename=m.empname
WHERE we.salary > wm.salary