[英]Merge 2 columns from different SQL server table with empty lines
I have this query to achieve my goal:我有这个查询来实现我的目标:
SELECT
Datum, LeavetypeID
FROM
Kalender
INNER JOIN
VerlofLijn ON Kalender.ID = VerlofLijn.DagID
WHERE
VerlofLijn.Persnummer = @pnummer;
So basically I have a calendar in the table Kalender
.所以基本上我在Kalender
表中有一个日历。
Every day has a unique id( Kalender.ID
).每天都有一个唯一的 id( Kalender.ID
)。 In a second table ( VerlofLijn
) the matching Kalender.ID
is stored in VerlofLijn.DagID
, together with the leavetype
and the unique employee number Persnummer
.在第二个表( VerlofLijn
)匹配Kalender.ID
存储在VerlofLijn.DagID
,与一起leavetype
和独特的雇员编号Persnummer
。
What I want to achieve is a query that loads all dates from the calendar, and - if the current logged-on employee has leave in the database - show this next to the correct date.我想要实现的是从日历中加载所有日期的查询,并且 - 如果当前登录的员工已在数据库中休假 - 将其显示在正确的日期旁边。
So if there is no leave at all in the database for this employee, I still need the calendar to show up so in a next step he can add leave to his personal calendar.因此,如果该员工的数据库中根本没有休假,我仍然需要显示日历,以便在下一步中他可以将休假添加到他的个人日历中。
I could create a personal calendar for every employee, but there has to be a better way, without the overhead of storing way to much data in the database, which will make the query to take much more time to search and complete.我可以为每个员工创建一个个人日历,但必须有更好的方法,没有在数据库中存储大量数据的开销,这将使查询花费更多的时间来搜索和完成。
Please try this query请试试这个查询
SELECT
Datum, LeavetypeID
FROM
Kalender
LEFT JOIN
VerlofLijn
ON Kalender.ID = VerlofLijn.DagID AND VerlofLijn.Persnummer = @pnummer;
Explanation: As you mentioned说明:如你所说
i want to achieve is a query that loads all dates from the calendar我想要实现的是一个从日历中加载所有日期的查询
you should use Kalender LEFT JOIN VerlofLijn
你应该使用Kalender LEFT JOIN VerlofLijn
Have you tried LEFT OUTER JOIN like this?你试过这样的 LEFT OUTER JOIN 吗?
SELECT
Datum, LeavetypeID
FROM
Kalender
LEFT OUTER JOIN
VerlofLijn ON Kalender.ID = VerlofLijn.DagID
WHERE
VerlofLijn.Persnummer = @pnummer;
You get all the values from Kalendar even if there are no matches from the table VerlofLijn, and all the common values from VerlofLijn.即使表 VerlofLijn 中没有匹配项,您也可以从 Kalendar 获取所有值,以及从 VerlofLijn 获取所有公共值。
You can find some examples here: What is the difference between "INNER JOIN" and "OUTER JOIN"?你可以在这里找到一些例子: “INNER JOIN”和“OUTER JOIN”有什么区别?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.