我有兩個表empmaster
和allocation
。我使用union
來執行sql操作,以便從兩個表中獲得結果。 empmaster
有empid
和其他empdetails
。表allocation
包含來自empmaster
作爲forkey密鑰另一個字段被稱爲per_alloc
。我需要檢索empdetails
其中滿足:不同字段的兩個select查詢的聯合
empmaster.empid
不allocation.empid
。empmaster.empid
inallocation.empid and allocation.per_alloc < 100
。我用
MySQL查詢是:
select distinct(tbl_empmaster.emp_fname)
from tbl_empmaster
where tbl_empmaster.emp_id not in(select tbl_allocation.emp_id
from tbl_allocation)
union
select distinct(tbl_empmaster.emp_fname)
from tbl_empmaster
where tbl_empmaster.emp_id in(select tbl_allocation.emp_id
from tbl_allocation
group by emp_id
having sum(per_alloc) < 100)
這僅檢索empdetails
,說tbl_empmaster.emp_fname
,我需要找回sum(per_alloc) from select tbl_allocation
!當我嘗試時會出現很多錯誤,請問任何人都可以告訴我正確的方法嗎?
第一句是一個查詢,沒有格式化,好心從 - 我有兩個表! –