我有两张桌子empmaster和allocation. 我曾经union为了从两个表中获取结果而进行 sql 操作。empmaster有empid等empdetails。表allocation包含作为empid外empmaster键的另一个名为 的字段per_alloc。我需要检索empdetails满足:
empmaster.empid不在allocation.empid。empmaster.empid在allocation.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!当我尝试它时会出现很多错误,请任何人告诉我正确的方法吗?