SQL查询以从同一表中获取员工姓名及其经理姓名

时间:2018-09-08 12:50:42

标签: sql sql-server select

员工表

Employee_id  Employee_name   Manager_id
-------------------------------------
Emp00001     Ram             Emp00005
Emp00002     Sharath         Emp00003
Emp00003     Nivas           Emp00005
Emp00004     Praveen         Emp00002
Emp00005     Maharaj         Emp00002

输出

Employee Name    Manager Name
------------------------------
Ram              Maharaj
Sharath          Nivas
Nivas            Maharaj
Praveen          Sharath
Maharaj          Sharath

employee表中,有三列Employee_idemployee_namemanager_id。从表中如何获取员工姓名和经理姓名?

7 个答案:

答案 0 :(得分:3)

您可以自联接表以从经理的ID中获取经理的姓名:

SELECT e.employee_name, m.employee_name AS manager_name
FROM   employee e
JOIN   employee m on e.manager_id = m.employee_id

答案 1 :(得分:1)

从员工中选择*


//Importing Modules
const { db } = require("../util/admin");

//retrieve all students

exports.getAllStudents = (req, res) => {
  db.collection("students")
    .orderBy("firstName", "asc")
    .get()
    .then((data) => {
      let students = [];
      data.docs.forEach((doc) => {
        students.push({
          studentId: doc.id,

          ...doc.data(),
        });
      });
      return res.json(students);
    })
    .catch((err) => console.error(err));
};


//get a student 

exports.getStudent = (req, res) => {
  let studentData = {};
  db.doc(`/students/${req.params.studentId}`).get()
    .then(doc => {
      if (!doc.exists) {
        return res.status(404).json({ error: 'Student not found' })
      }
      studentData = doc.data();
      return res.json(studentData)
    }).catch(err => {
      console.error(err);
    })
}

//update a student

exports.updateStudent = (req, res) => {
  let studentData = {};
  db.doc(`/students/${req.params.studentId}`)
    .update(req.body)
    .then(doc => {
      studentData = doc.data();
      console.log('studentData.studentId:', studentData.studentId)
      return res.json(studentData)
    }).catch(err => {
      console.error(err);
    })
}


//create a new student

exports.createStudent = (req, res) => {
  const newStudent = {
    country: req.body.country,
    experience: req.body.experience,
    studentId: req.body.studentId //should I change this?
  };

  db.collection("students")
    .add(newStudent)
    .then((doc) => {
      res.json({ message: `document ${doc.id} created sucessfully` });
    })
    .catch((err) => {
      res.status(500).json({ error: "something went wrong" });
      console.error(err);
    });
};

enter image description here

答案 2 :(得分:0)

希望经理表将在您的数据库中可用

SELECT employee_name, M.manager_name
FROM   employee e
INNER JOIN tableManager M ON e.ManagerID = M.MangerID

答案 3 :(得分:0)

必填表:-员工。

查询:-

SELECT e.Employee Name,
       e.Employee Name as Manager Name
  FROM Employee e JOIN Employee m 
    ON e.Employee id = m.Manager id

说明:-

通过给出此条件“ Employee e JOIN Employee m”,它将自动将单个表视为“ e”和“ m”这两个不同的表,然后将表e中的Employee ID与表m的经理ID进行比较。 只要找到两个ID都相同的匹配项,就会添加到结果中。

答案 4 :(得分:0)

请尝试这个

SELECT employee_name AS Employee_Name,(SELECT employee_name FROM   employee  where
employeeid=ManagerID ) AS Manager_Name  FROM   employee 

答案 5 :(得分:0)

enter image description here

在下面的查询中,您可以在这里获取Employeename和ManagerName,我只有一个表EmpMgr: 从EmpMgr中选择e.employeename作为ename,e.managerid作为mgrid,e1.employeename作为经理名称e在e.managerid = e1.employeeid上加入EmpMgr e1

注意:您可以使用左联接获得所有雇员姓名,而与经理姓名无关

从EmpMgr中选择e.employeename作为ename,e.managerid作为mgrid,e1.employeename作为经理名称e在e.managerid = e1.employeeid上离开EmpMgr e1

答案 6 :(得分:0)

你好朋友,请查看以下解决方案。

这是结果截图: enter image description here

这是查询:

SELECT e.employee_name,m.Employee_name FROM   EmpTable e 
    INNER JOIN EmpTable m 
            ON M.Employee_id =e.manager_id