Sql 员工薪资查询错误

Sql 员工薪资查询错误,sql,Sql,我正试图为一家公司制作一份工资报告,我想通过如下查询显示该公司的所有员工: select tab1.name, tab1.job,tab1.salary, tab2.discount-days, (tab1.salary-((tab1.salary/30)*tab2.discount-days) as 'total' from tab1, tab2 where tab2 .date_discount between date_begin and date_

我正试图为一家公司制作一份工资报告,我想通过如下查询显示该公司的所有员工:

select tab1.name, tab1.job,tab1.salary, tab2.discount-days,
       (tab1.salary-((tab1.salary/30)*tab2.discount-days) as 'total' 
from   tab1, 
       tab2 
where  tab2 .date_discount between date_begin and date_end;
这里只显示了在这段时间内享受折扣的员工,但我想显示公司的所有员工,如果有人享受了折扣,也要显示总数


请需要帮助来执行查询,该查询不仅显示有折扣的所有员工

您还需要使用LEFT JOIN

select tab1.name, tab1.job,tab1.salary, tab2.discount-days,
       (tab1.salary-((tab1.salary/30)*tab2.discount-days) as 'total' 
from   tab1, 
       tab2 left join tab2  as tab3 on tab2.primary_key = tab3.id.primary_key 
and tab3 .date_discount between date_begin and date_end;
如果使用左联接,左侧表中存在的所有记录都将出现,而右侧表中匹配的记录也将出现


我想它可能适合您。

您能提供模式和一些示例数据吗?问题中没有tab3。但是,是的,我同意他需要在tab1和tab2之间使用左连接。是的,我刚刚注意到tab3的别名。但是我还是认为tab1和tab2之间的左连接就足够了。@gefrag但是没有提到任何关系。真的,但我想一定有关系。gefrag&PSR,你能写最后一个查询吗?因为我还是初学者,连接不好