如何在MySQL中获取主管的名称而不是UserID + INNER JOIN

时间:2019-12-11 09:19:48

标签: mysql

这是我的桌子:

  

从用户中选择*

USERID  NAME    STATUS
1       AAA     Member
2       BBB     Member
3       CCC     Supervisor
4       DDD     Member
5       EEE     Member
  

从出勤中选择*

NO  DATE        SUPERVISOR  MEMBER
1   2019-12-11  3           1
2   2019-12-11  3           2
3   2019-12-11  3           4
4   2019-12-11  3           5
  

从详细信息中选择*

NO  USERID  ATTENDANCE  REASON
1   1       0           SICK
2   2       1           -
3   4       1           -
4   5       1           -

我想要的结果:

USERID  DATE        NAME    REASON  SUPERVISOR
1       2019-12-11  AAA     SICK    CCC
2       2019-12-11  BBB     -       CCC
3       2019-12-11  DDD     -       CCC
4       2019-12-11  EEE     -       CCC

我尝试过的SQL调用主管的用户ID,而不是主管的名称。

SELECT d.userid, a.date, u.name, d.reason, a.supervisor FROM attendance a INNER JOIN detail d on a.userid = d.userid INNER JOIN user u on d.userid = u.userid WHERE d.attendance=0

我需要有关sql的帮助,以调用主管的名称而不是userid。 谢谢。

2 个答案:

答案 0 :(得分:1)

您必须两次连接用户表,因为您要同时为其成员名和主管名进行匹配。我还使用了console.log(this.myComputedProperty) this.myComputedProperty = 'new value!' console.log(this.myComputedProperty) ,因为有时原因没有值,但是您仍然想从其他表中提取记录。

protocol PrintSomething {
    //declaration
    func printMyname(name: String)
}

class Display: PrintSomething {
    //defination
    func printMyname(name: String) {
        print(name)
    }


     var myProtocol: PrintSomething?

    func somethingToDisplay() {
        myProtocol?.printMyname(name: "Ygor")
    }
}

答案 1 :(得分:-1)

SELECT u1.userid, a.date, u1.name As Name, d.reason, u2.name AS supervisor
FROM user u1
LEFT JOIN attendance a ON u1.userid = a.member
LEFT JOIN detail d ON u1.userid = d.userid
LEFT JOIN user u2 ON a.supervisor = u2.userid
WHERE d.attendance=0;