2017-07-04 226 views
0

第一臺員工查詢SQL連接和WHERE子句

id empName  empEmail   empSal active 
==================================================== 
13 sadf  [email protected]  233  1 
12 checkout [email protected] 3   1 
11 safg  [email protected]  123  1 
10 sdf   [email protected]   4   1 
9 sdaf  [email protected]  3   1 
8 asdf  [email protected]  8   1 
7 asdf  [email protected]  8   1 
6 asdf  [email protected]  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,我們想要什麼,應該不能算如果我有零,它應該幫助執行。這怎麼可能?

+0

你可以闡述多個表的條件我不明白嗎?你想跳過那些有0狀態的員工? –

+0

請張貼預期結果。 – maSTAShuFu

+0

如果要計數,請添加'SELECT count(o.id)AS cnt' –

回答

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 
+0

查看第二個表emp_status emp_id具有活動值0它不應該顯示,但仍顯示 –

+0

要獲取emp.active = NULL? –

+0

不起作用請檢查 –