ACCESS SQL:即使没有与其他表连接的数据,如何检索表的所有数据

时间:2012-09-26 09:25:02

标签: sql ms-access

我有一份工作的SQL声明用于计算工资和计算工作日的数量,SO的一位成员在这里帮助我解决了这个问题。然而,问题是结果只出现在那个时期有效的劳动力。我真正想要的是结果必须显示所有劳动力(在tbl_labor),无论他是否在那个时期工作过。

select
  lab.lb_name,
  max(days),
  sum(prod.pdtn_qty * pdWk.pd_cost / 12) as Total 
from
  tbl_labor as lab,
  tbl_production as prod,
  tbl_pdWk_process as pdwk,

(select pdn2.lb_id, count(pdn2.lb_id) as days from
  (select  lb_id
  from tbl_production  pdn1
  where pdn1.pdtn_date >= #9/5/2012#
  and   pdn1.pdtn_date <= #2012-09-20#
  group by lb_id, pdtn_date ) as pdn2
     group by pdn2.lb_id) as pdn3

where prod.pdtn_date >= #9/5/2012#
and   prod.pdtn_date <= #2012-09-20#
and prod.lb_id = lab.lb_id
and   prod.pd_making_id = pdwk.pd_making_id
and lab.lb_id = pdn3.lb_id
group by lab.lb_name

以下是我的相关表格:

tbl_labor
 lb_id | lb_name  | lb_OT ($/day) | If_social_sec
   1   |  John    |    10         |    yes
   2   |  Mary    |    10         |    no
   3   |  Jane    |    10         |    no

tbl_production 
pdtn_date  | lb_id  | pdtn_qty(pcs) | pd_making_id 
 5/9/12    |   1    |     200       |    12
 5/9/12    |   1    |     40        |    13
 5/9/12    |   2    |     300       |    12
 7/9/12    |   1    |     48        |    13
 13/9/12   |   2    |     220       |    14
 15/9/12   |   1    |     20        |    12
 20/9/12   |   1    |     33        |    14
 21/9/12   |   2    |     55        |    14
 21/9/12   |   1    |     20        |    12

tbl_pdWk_process 
pd_making_id | pd_cost($/dozen) | pd_id 
    12       |     2            |   001
    13       |     5            |   001
    14       |     6            |   002

1 个答案:

答案 0 :(得分:4)

替换旧式连接

from
  tbl_labor as lab,
  tbl_production as prod,
...
where 
  lab.fld1 = prod.fld1

更新的LEFT JOIN

from
  tbl_labor as lab
  LEFT JOIN tbl_production as prod ON lab.fld1 = prod.fld1
...

编辑:
在原始代码中

from
  tbl_labor as lab,
  tbl_production as prod
...
where ...
prod.lb_id = lab.lb_id
...

在tbl_production和tbl_labor表之间创建内部联接。 它需要用更新的LEFT JOIN

替换
from
  tbl_labor as lab
  LEFT JOIN tbl_production as prod ON lab.lb_id = prod.lb_id
...

您可以在http://www.w3schools.com/sql/sql_join.asp

上阅读有关SQL JOIN的信息