简体   繁体   English

用空行合并来自不同 SQL 服务器表的 2 列

[英]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.

相关问题 在 SQL 服务器中显示来自同一表的不同属性的列 - Showing columns of different attributes from the same table in SQL Server SQL Server 游标从不同的表中提取数据并合并相同的行 - SQL Server cursor to pull data from different tables and merge the same lines SQL合并现有表中的列 - SQL merge Columns from existing table 将几列从project_1表复制到临时表,但临时表在sql server中返回空 - copying few columns from project_1 table to temp table, but temp table return empty in sql server SQL Update 2来自不同数据库(同一服务器)的具有相似表(不同列)的不同列 - SQL Update 2 different columns with similar table(not the same columns) from different database (same server) 打印SQL Server数据库中每个表的前10个条目,表之间空行 - Print first 10 entries from each table in SQL Server database with empty lines between tables SQL Server映射具有不同表列的行 - SQL Server map rows with columns of different table 合并两个SQL查询结果中的列数不同的列 - Merge columns from two SQL query results with different number of columns SQL Server 2016:将多行转换为具有不同迭代的列 - SQL Server 2016: Turn multiple lines into columns with the different iterations 如何将数据从源表(但不同的列)插入SQL服务器中的两个不同的表中 - How to insert data from source table (but different columns) into two different tables in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM