通过查询和连接访问(sql)组

时间:2012-07-24 14:58:35

标签: sql ms-access

我有两个数据表,

 Project (ID, NAME, price),
 Expenses (ID, NAME, price, PROJECT_ID,)

我想找到所有带有项目费用摘要的项目 我尝试了这个查询,但我无法获得所有项目,只有他有的费用

SELECT PROJECT.p_id, SUM(EXDE.EXD_AMOUNT) AS T
FROM  PROJECT 
INNER JOIN EXDE
    ON PROJECT.p_id = EXDE.P_NO
GROUP BY  PROJECT.p_id

我非常感谢任何答案都能帮助我找到解决方案

2 个答案:

答案 0 :(得分:2)

内部联接仅选择两个表中的匹配项。你需要左加入。

SELECT PROJECT.p_id, SUM(EXDE.EXD_AMOUNT) AS T
FROM  PROJECT 
LEFT JOIN EXDE
    ON PROJECT.p_id = EXDE.P_NO
GROUP BY  PROJECT.p_id

答案 1 :(得分:0)

改为使用LEFT JOIN

SELECT   PROJECT.p_id, SUM(EXDE.EXD_AMOUNT) AS T
  FROM   PROJECT 
             LEFT JOIN EXDE
               ON PROJECT.p_id = EXDE.P_NO
GROUP BY  PROJECT.p_id