How do I join two tables with different column in SQL Server CE?

时间秒杀一切 提交于 2019-12-22 14:56:10

问题


How can I joining two tables with different column in SQL Server CE?

I have two tables:

Table Schedule

+-----+----------+------+
+ ID  + Name     + Type +
+-----+----------+------+
+ 1   + A        + 1    +
+ 2   + B        + 1    +
+ 3   + C        + 2    +
+-----+----------+------+

Table Description

+-----+--------------------+
+ ID  + Description        +
+-----+--------------------+
+ 1   + A1 - XXXXX         +
+-----+--------------------+

And what I want to get is the table like:

+-----+----------+-----------------+
+ ID  + Name     + Description     +
+-----+----------+-----------------+
+ 1   + A        + A1 - XXXXX      +
+ 2   + B        + -               +
+ 3   + C        + -               +
+-----+----------+-----------------+

Where the Description column should be filled in by - when the ID is not on the Description table.

I tried this code:

SELECT S.ID, D.Description 
FROM Schedule AS S 
INNER JOIN Description AS D

But resulted in:

+-----+----------+-----------------+
+ ID  + Name     + Description     +
+-----+----------+-----------------+
+ 1   + A        + A1 - XXXXX      +
+ 2   + B        + A1 - XXXXX      +
+ 3   + C        + A1 - XXXXX      +
+-----+----------+-----------------+

And when I tried to give ON Clause:

SELECT S.ID, D.Description 
FROM Schedule AS S 
INNER JOIN Description AS D ON S.ID = D.ID

It just get the row where the ID is on the Description table, like:

+-----+----------+-----------------+
+ ID  + Name     + Description     +
+-----+----------+-----------------+
+ 1   + A        + A1 - XXXXX      +
+-----+----------+-----------------+

How can I do that?


[UPDATE]

I tried this code and it works:

SELECT  S.ID, S.Name, COALESCE (D.Description, '-') AS Description
FROM Schedule AS S 
LEFT OUTER JOIN Description AS D ON S.ID = D.ID

But now, how can I add a WHERE clause on it (pls see table SCHEDULE above)?

I tried:

SELECT  S.ID, S.Name, COALESCE (D.Description, '-') AS Description
FROM Schedule AS S 
LEFT OUTER JOIN Description AS D ON S.ID = D.ID AND S.Type = '1'

But still get the whole rows.


回答1:


LEFT OUTER JOIN in SQL Server CE

You need to use LEFT OUTER JOIN to join the tables Schedule and Description on the key field ID. Also, use COALESCE to replace NULL values in Description column with -

Script:

SELECT          S.ID
            ,   S.Name
            ,   COALESCE (D.Description, '-') AS Description
FROM            Schedule AS S 
LEFT OUTER JOIN Description AS D 
ON              S.ID = D.ID

Output:

Tested in Microsoft SQL Server CE version 4.0.8482.1

With WHERE clause

You need add the WHERE clause after the JOINs. If you are planning to having an ORDER BY, it should come after the WHERE clause.

Script:

SELECT          S.ID
            ,   S.Name
            ,   COALESCE (D.Description, '-') AS Description
FROM            Schedule    AS S 
LEFT OUTER JOIN Description AS D 
ON              S.ID = D.ID
WHERE           (S.Type = 1)

Output:

Tested in Microsoft SQL Server CE version 4.0.8482.1




回答2:


You need LEFT OUTER JOIN.

SQLFiddle: http://sqlfiddle.com/#!3/7dccf/1

Query:

select s.ID, s.Name, Coalesce(d.description,'-') as description
from schedule s 
left outer join description d on d.id = s.id



回答3:


SELECT S.ID, D.Description 
FROM Schedule AS S FULL OUTER JOIN Description AS D
ON S.ID = D.ID

INNER JOIN means select only rows that exist in both tables.
FULL OUTER JOIN means select rows as long as they exist in one table.

You need FULL OUTER JOIN...




回答4:


You need to use an OUTER JOIN instead of INNER, and use IsNull or Coalesce to replace NULL values:

SELECT S.ID, Coalesce(D.Description , '-') AS Description
FROM Schedule AS S FULL OUTER JOIN Description AS D
ON S.ID = D.ID


来源:https://stackoverflow.com/questions/10491802/how-do-i-join-two-tables-with-different-column-in-sql-server-ce

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!