簡體   English   中英

查詢sql join和where子句

[英]query sql join and where clause

第一表員工

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)應該有助於執行。 怎么可能呢?

不要在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM