1

我的桌子是“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`

但它不工作......

4

6 回答 6

3

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

类似的东西?

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)
于 2013-06-27T09:58:38.577 回答
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'
于 2013-06-27T09:58:39.927 回答
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 演示

于 2013-06-27T10:13:17.407 回答
0

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

SELECT ID,DEPT_ID,ENAME,DESIGNATION,
case 
when DEPT_ID=12 then 'B'
when DEPT_ID=14 then 'D'
end "MANAGER"
from DESIGNATION
于 2013-06-27T10:09:20.373 回答
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";
}
于 2013-06-27T10:00:13.857 回答
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'
于 2013-06-27T10:02:33.430 回答