Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MYSQL检查是否所有表1条目都有表2条目_Mysql_Sql - Fatal编程技术网

MYSQL检查是否所有表1条目都有表2条目

MYSQL检查是否所有表1条目都有表2条目,mysql,sql,Mysql,Sql,我有两张员工表和证书。现在我有4个证书,我想检查每个员工哪些证书通过了,哪些没有通过 Select Distinct(certificate.certificatename) FROM `employeemanagement$certificate` certificate Where certificatename = '**********' or certificatename = '************' or certificatename = '************' or

我有两张员工表和证书。现在我有4个证书,我想检查每个员工哪些证书通过了,哪些没有通过

Select Distinct(certificate.certificatename)
FROM `employeemanagement$certificate` certificate
Where certificatename = '**********' 
or certificatename = '************'
or certificatename = '************'
or certificatename = '************'
这将给我4个我想要的证书

SELECT employees.employeeid, employees.employeename, certificate.certificatename, certificate.expirationdate, certificate.`status`
FROM `employeemanagement$employees` employees 
LEFT JOIN `employeemanagement$certificate_employees` certifcate_employees on employees.id = certifcate_employees.`employeemanagement$employeesid`
LEFT JOIN `employeemanagement$certificate` certificate on certificate.id = certifcate_employees.`employeemanagement$certificateid`
Where employees.currentstatus = 'Active'

这会给我所有员工的所有证书,但不会给我每个证书的结果

我认为条件聚合是一种合理的方法:

SELECT e.employeeid, e.employeename, 
       MAX(CASE WHEN c.certificatename = 'C1' THEN  c.expirationdate END) as c1_expiration,
       MAX(CASE WHEN c.certificatename = 'C1' THEN  c.staus END) as c1_status, 
       MAX(CASE WHEN c.certificatename = 'C2' THEN  c.expirationdate END) as c2_expiration,
       MAX(CASE WHEN c.certificatename = 'C2' THEN  c.staus END) as c2_status, 
       MAX(CASE WHEN c.certificatename = 'C3' THEN  c.expirationdate END) as c3_expiration,
       MAX(CASE WHEN c.certificatename = 'C3' THEN  c.staus END) as c3_status, 
       MAX(CASE WHEN c.certificatename = 'C4' THEN  c.expirationdate END) as c4_expiration,
       MAX(CASE WHEN c.certificatename = 'C4' THEN  c.staus END) as c4_status
FROM `employeemanagement$employees` e LEFT JOIN 
     `employeemanagement$certificate_employees` ce
      ON e.id = ce.`employeemanagement$employeesid` LEFT JOIN
     `employeemanagement$certificate` c
     ON c.id = ce.`employeemanagement$certificateid`
WHERE e.currentstatus = 'Active'
GROUP BY e.employeeid;

您可能会发现,将所有证书汇总到每名员工的一行,而不是每名证书的多行,这样做会更轻松。

删除
Where employees.currentstatus='Active'
并运行查询时会发生什么?然后它将显示所有员工。这没关系,因为我只需要在职员工的记录