第一名员工
id empName empEmail empSal active
====================================================
13 sadf asdf@sdf.com 233 1
12 checkout cherckout@g.com 3 1
11 safg asdf@asdf.com 123 1
10 sdf sadf@sdf 4 1
9 sdaf asdf@sdf.com 3 1
8 asdf asdf@sdf.com 8 1
7 asdf asdf@sdf.com 8 1
6 asdf asdf@sdf.com 8 1
第二张表emp_status
id emp_id EmpSalary active
=============================
5 4 156 1
4 2 156 1
3 2 555 1
2 1 200 0
1 1 1500 1
SELECT o.*
FROM employees as o
LEFT JOIN emp_status as emp ON emp.emp_id = o.id
WHERE (emp.active IS NULL OR (emp.active != 0))
GROUP BY o.id
ORDER BY o.id DESC
如果emp_status有两行,其中一行的状态为1,另一行的状态为0,我们想要它不应该计为1,如果我有零,它应该有助于执行。怎么可能呢?
答案 0 :(得分:2)
不要在where子句上传递连接表条件,我建议你激活列数据类型= ENUM并设置值0,1,2,3,4,5,6
SELECT o.* FROM employees as o
LEFT JOIN emp_status as emp ON emp.emp_id = o.id and emp.active IN (1,2,3,5,6) GROUP BY o.id ORDER BY o.id DESC