php 即使表2中没有链接到表1的数据,也从表中获取数据

wfveoks0  于 2022-12-02  发布在  PHP
关注(0)|答案(1)|浏览(136)

我正在创建一个计划者,但是我现在所拥有的SQL语句只显示在表2(plannerEntries)中有内容的雇员,而不显示表1(Employee_List)中的其余雇员。
我需要将所有雇员输出到表中,而不管他们是否有任何工作分配给他们的一周,以便他们可以有新的工作分配容易。
这是我当前的SQL代码

SELECT [EL].[Employee_Numer], 
       [PP].[workDate], 
       [PP].[jobNo],    
       [PP].[workDescription], 
       [PP].[timeOfDay], 
       [JF].[Description], 
       [EL].[Forename], 
       [EL].[Surname]
FROM   plannerEntries AS PP
  RIGHT JOIN [Employee_List] AS EL
    ON [PP].[employeeNumber] = [EL].[Employee_Numer]
  INNER JOIN [Job File] AS JF
    ON [PP].[jobNo] = [JF].[Job No]
WHERE [PP].[workDate] >= '$monday'
AND   [PP].[workDate] <= '$sunday'
ORDER BY [PP].[employeeNumber] ASC;

我希望打印所有员工而不考虑表2中的记录,但只打印了表2中有记录的员工。下图是实际输出。

kuarbcqp

kuarbcqp1#

请检查内部联接、左联接和右联接之间的区别。
像这样的东西应该可以满足你的需要:

SELECT
    [EL].[Employee_Numer],
    [PP].[workDate],
    [PP].[jobNo],
    [PP].[workDescription],
    [PP].[timeOfDay],
    [JF].[Description],
    [EL].[Forename],
    [EL].[Surname]
FROM
    [Employee_List] AS EL
    left join plannerEntries AS PP on [PP].[employeeNumber] = [EL].[Employee_Numer]
        and [PP].[workDate] >= '$monday'
        and [PP].[workDate] <= '$sunday'
    left join [Job File] AS JF on [JF].[Job No] = [PP].[jobNo]
ORDER BY
    [PP].[employeeNumber] ASC;

相关问题