找到销售员工的经理

时间:2013-06-27 09:51:25

标签: mysql sql

我的桌子是'DESIGNATION'

ID    ||    DEPT_ID     ||      E_NAME       ||      DESIGNATION
1     ||     12         ||        A          ||       EMPLOYEE
2     ||     12         ||        B          ||       MANAGER
3     ||     12         ||        C          ||       EMPLOYEE
4     ||     14         ||        D          ||       MANGER
5     ||     14         ||        E          ||       EMPLOYEE
6     ||     14         ||        F          ||       EMPLOYEE

我希望管理员通过他们的DEPT_NAME ....

平均结果将如下所示

ID    ||    DEPT_ID     ||      E_NAME       ||      DESIGNATION  ||   MANAGER
1     ||     12         ||        A          ||       EMPLOYEE    ||    B
2     ||     12         ||        B          ||       MANAGER     ||    B
3     ||     12         ||        C          ||       EMPLOYEE    ||    B
4     ||     14         ||        D          ||       MANGER      ||    D
5     ||     14         ||        E          ||       EMPLOYEE    ||    D
6     ||     14         ||        F          ||       EMPLOYEE    ||    D

我的查询是

SELECT `ID`,`DEPT_ID`,`ENAME`,`DESIGNATION`,
    (select `ENAME` from `DESIGNATION` where 
        (select `E_NAME` from `DESIGNATION` where 
            (SELECT `DEPT_ID` FROM `DESIGNATION` WHERE `DESIGNATION` = 'EMPLOYEE')
            =
            (SELECT `DEPT_ID` FROM `DESIGNATION` WHERE `DESIGNATION` = 'MANAGER') and `DESIGNATION`='MANAGER')
    AS MANAGER
from `DESIGNATION`

但它不起作用......

6 个答案:

答案 0 :(得分:3)

你只需要一个JOIN operation。这是使用数据库时的基本概念。你应该花些时间阅读它。

那样的东西?

SELECT A.*, B.E_NAME 
   FROM DESIGNATION AS A, DESIGNATION AS B
   WHERE B.DESIGNATION = "MANAGER" 
     AND A.DEPT_ID = B.DEPT_ID

或使用显式 JOIN语法:

SELECT A.*, B.E_NAME 
   FROM DESIGNATION AS A JOIN DESIGNATION AS B USING (DEPT_ID)
   WHERE B.DESIGNATION = "MANAGER" 

修改 如果您可以拥有多个管理员,则可以使用GROUP_CONCAT聚合函数和E_NAME显式组(假设这是一个唯一键):

SELECT A.*, GROUP_CONCAT(B.E_NAME) 
   FROM DESIGNATION AS A, DESIGNATION AS B
   WHERE B.DESIGNATION = "MANAGER" 
     AND A.DEPT_ID = B.DEPT_ID
   GROUP BY(A.E_NAME)

答案 1 :(得分:1)

旧式连接语法 - 抱歉 - 但您的子查询没有多大意义。

Select d.ID,
       d.DEPT_ID,
       d.E_NAME, 
       d.DESIGNATION
       m.MANAGER
from   designation d, 
       designation m
where  d.dept_id   =   m.dept_id
and    m.designation = 'MANAGER'

答案 2 :(得分:0)

对于您正在运行的任何服务器端脚本,这看起来更好。例如,在PHP中,您可以这样做:

$managers = []; // array() before version 5.4
$employees = []; // see above comment
$query = "SELECT * FROM `DESIGNATION`";
$result = mysql_query($query); // adjust according to extension of choice
while($row = mysql_fetch_assoc($result)) { // same as previous comment
    $employees[] = $row;
    if( $row['DESIGNATION'] == "MANAGER") $managers[$row['DEPT_ID']] = $row['E_NAME'];
}
foreach($employees as $i=>$e) {
    $employees[$i]['MANAGER'] = $managers[$e['DEPT_ID']] ?: "Nobody";
}

答案 3 :(得分:0)

简单的JOIN会有所帮助,我更喜​​欢显式JOIN而不是隐式逗号表示法:

SELECT `ID`,`DEPT_ID`,`E_NAME`,`DESIGNATION`, m.e_name, AS `MANAGER`
FROM `DESIGNATION` e
INNER JOIN `DESIGNATION` m 
 ON e.dept_id = m.dept_id 
WHERE m.designation = 'MANAGER'

答案 4 :(得分:0)

我们不能简单地以更简单的方式做到这一点吗?

SELECT ID,DEPT_ID,ENAME,DESIGNATION,
case 
when DEPT_ID=12 then 'B'
when DEPT_ID=14 then 'D'
end "MANAGER"
from DESIGNATION

答案 5 :(得分:0)

尝试以下

SELECT t.id
       ,t.dept_id
       ,t.e_name
       ,t.designation
       ,ta.e_name As Manager   FROM Table1 t   JOIN (SELECT e_name,dept_id 
          FROM Table1 WHERE designation  = 'MANAGER' 
          GROUP BY dept_id,e_name) ta    ON ta.dept_id = t.dept_id

<强> SQLFiddle Demo