简体   繁体   English

从多个表中选择SQL Server

[英]SQL Server SELECT from multiple tables

I'm creating a timesheet app and have the following tables... 我正在创建时间表应用,并具有以下表格...

tblTimeEntry : tblTimeEntry

-entryID (int)
-entryDate (datetime)
-enteredDate (datetime)
-entryUser (int)
-entryJob (int)
-entryTask (int)
-entryWeekNo (int)

tblWagesWeeks : tblWagesWeeks

-weekID (int)
-weekDay (int)
-date (datetime)

tblWagesWeeks contains weekID which is an int assigning an int to each week. tblWagesWeeks包含weekID ,这是一个为每个星期分配一个int的int。 weekDay is an int from 1-7 allocating an int to each day of the week. weekDay是从1到7的整数,将整数分配给一周中的每一天。 Three years worth of dates have been entered into tblWagesWeeks with the corresponding weekDay numbers and weekID numbers. 已经将具有三年的日期以及相应的weekDay号和weekID号输入到tblWagesWeeks

tblTimeEntry is used to record time entries which are allocated to a date. tblTimeEntry用于记录分配给日期的时间条目。

tblTimeEntry.entryWeekNo matches tblWagesWeeks.weekID and is an int to represent the week number. tblTimeEntry.entryWeekNo匹配tblWagesWeeks.weekID并代表周数的int。 tblTimeEntry.entryDate matches tblWagesWeeks.date and is a datetime . tblTimeEntry.entryDate匹配tblWagesWeeks.date ,是一个datetime

So for the front end I display a listview displaying the weeks. 因此,对于前端,我显示一个显示星期的列表视图。 The users clicks on a week and the days in that week are displayed. 用户单击一周,然后显示该周中的日期。 When the user clicks on a day they are able to enter a time entry. 当用户单击一天时,他们可以输入时间条目。

The problem I have is when I try to list the time entries for a week using the following query... 我遇到的问题是当我尝试使用以下查询列出一周的时间条目时...

SELECT 
    *,   
    CONVERT(varchar(19),CONVERT(date,date,106),103) AS dateFormatted,
    CASE 
       WHEN entryHalfDay = '1' 
          THEN 'Half Day' 
       WHEN entryHalfDay = '0' 
          THEN '' 
    END AS [entryHalfDayString], 
    CASE 
       WHEN entryFullDay = '1' 
         THEN 'Full Day' 
       WHEN entryFullDay = '0' 
         THEN '' 
    END AS [entryFullDayString], 
    CASE 
       WHEN enteredDate IS NULL 
          THEN '' 
          ELSE 'Entry Complete' 
    END AS dayStatus 
FROM 
    tblWagesWeeks AS a 
LEFT JOIN 
    tblTimeEntry AS b ON a.[date] = b.entryDate 
WHERE 
    (entryUser = '1' OR entryUser IS NULL) AND weekID = '25'

The problem I have is that if the user I'm searching on ('1' in the query above) does not have a time entry on a particular date but another user does, the empty date is not show in the list so the user does not have an opportunity to enter time for that day 我的问题是,如果我正在搜索的用户(上述查询中的“ 1”)没有在特定日期输入时间,但是另一位用户输入了时间,则该列表中不会显示空日期,因此该用户没有机会输入当天的时间

Below is output from the query above to demonstrate... 下面是上面查询的输出,以演示...

weekID  weekDay date    entryID entryDate   enteredDate entryUser   entryJob    entryTask   entryFullDay    entryHalfDay    entryWeekNo dateFormatted   entryHalfDayString  entryFullDayString  dayStatus
25  1   2015-06-15  23  2015-06-15  2015-07-14  1   5   5   1   0   25  15/06/2015      Full Day    Entry Complete
25  2   2015-06-16  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    16/06/2015  NULL    NULL    
25  3   2015-06-17  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    17/06/2015  NULL    NULL    
25  4   2015-06-18  26  2015-06-18  2015-07-14  1   2   2   1   0   25  18/06/2015      Full Day    Entry Complete
25  5   2015-06-19  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    19/06/2015  NULL    NULL    
25  7   2015-06-21  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    21/06/2015  NULL    NULL    

As you can see, day 6 is missing. 如您所见,第6天丢失了。

When I search for the same week using entryUser = 25 you can see that there is a entry for day 6... 当我使用entryUser = 25搜索同一周时,您会看到第6天有一个条目...

SELECT *, CONVERT(varchar(19),CONVERT(date,date,106),103) AS dateFormatted,
CASE WHEN entryHalfDay='1' THEN 'Half Day' WHEN entryHalfDay='0' THEN '' END AS [entryHalfDayString], 
CASE WHEN entryFullDay='1' THEN 'Full Day' WHEN entryFullDay='0' THEN '' END AS [entryFullDayString], 
CASE WHEN enteredDate IS NULL THEN '' ELSE 'Entry Complete' END AS dayStatus 
FROM tblWagesWeeks AS a LEFT JOIN tblTimeEntry AS b ON a.[date] = b.entryDate 
WHERE (entryUser = '25' OR entryUser IS NULL) AND weekID = '25'

weekID  weekDay date    entryID entryDate   enteredDate entryUser   entryJob    entryTask   entryFullDay    entryHalfDay    entryWeekNo dateFormatted   entryHalfDayString  entryFullDayString  dayStatus
25  1   2015-06-15  23  2015-06-15  2015-07-14  1   5   5   1   0   25  15/06/2015      Full Day    Entry Complete
25  2   2015-06-16  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    16/06/2015  NULL    NULL    
25  3   2015-06-17  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    17/06/2015  NULL    NULL    
25  4   2015-06-18  26  2015-06-18  2015-07-14  1   2   2   1   0   25  18/06/2015      Full Day    Entry Complete
25  5   2015-06-19  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    19/06/2015  NULL    NULL    
25  7   2015-06-21  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    21/06/2015  NULL    NULL    

so I need to know how I can always show all week days in the list, blank and populated for a single user and a single week number. 因此,我需要知道如何始终在列表中显示所有工作日,将其空白显示并填充为单个用户和单个星期编号。

Any help would be massively appreciated. 任何帮助将不胜感激。

You need to make the user condition of the join and the week a condition of the main table like this: 您需要像这样将联接的用户条件和星期作为主表的条件:

SELECT *, CONVERT(varchar(19),CONVERT(date,date,106),103) AS dateFormatted,
CASE WHEN entryHalfDay='1' THEN 'Half Day' WHEN entryHalfDay='0' THEN '' END AS [entryHalfDayString], 
CASE WHEN entryFullDay='1' THEN 'Full Day' WHEN entryFullDay='0' THEN '' END AS [entryFullDayString], 
CASE WHEN enteredDate IS NULL THEN '' ELSE 'Entry Complete' END AS dayStatus 
FROM tblWagesWeeks AS a 
LEFT JOIN tblTimeEntry AS b ON a.[date] = b.entryDate AND b.entryUser = 25
WHERE a.weekID = '25'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM