2

如何在 SQL Server CE 中加入具有不同列的两个表?

我有两张桌子:

时间表

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

表格说明

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

我想要得到的是这样的表格:

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

当ID不在表上时,该Description列应填写在哪里。-Description

我试过这段代码:

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

但结果是:

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

当我试图给出 ON 子句时:

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

它只是获取桌子ID上的行,例如:Description

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

我怎样才能做到这一点?


[更新]

我尝试了这段代码,它可以工作:

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 子句(请参见上表SCHEDULE)?

我试过了:

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'

但仍然得到整行。

4

4 回答 4

2

SQL Server CE 中的左外连接

您需要使用键字段IDLEFT OUTER JOIN来连接表ScheduleDescription。此外,用于将描述列中的 NULL 值替换为COALESCE-

脚本:

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

输出:

经测试Microsoft SQL Server CE version 4.0.8482.1

输出

带有 WHERE 子句

您需要在JOINWHERE之后添加子句。如果你打算有一个,它应该在子句之后。ORDER BYWHERE

脚本:

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)

输出:

经测试Microsoft SQL Server CE version 4.0.8482.1

输出在哪里

于 2012-05-08T03:17:50.187 回答
2

你需要LEFT OUTER JOIN.

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

询问:

select s.ID, s.Name, Coalesce(d.description,'-') as description
from schedule s 
left outer join description d on d.id = s.id
于 2012-05-08T03:05:44.633 回答
1

您需要使用 anOUTER JOIN而不是INNER,并使用IsNullorCoalesce替换NULL值:

SELECT S.ID, Coalesce(D.Description , '-') AS Description
FROM Schedule AS S FULL OUTER JOIN Description AS D
ON S.ID = D.ID
于 2012-05-08T02:48:22.030 回答
1
SELECT S.ID, D.Description 
FROM Schedule AS S FULL OUTER JOIN Description AS D
ON S.ID = D.ID

INNER JOIN表示仅选择两个表中都存在的行。
FULL OUTER JOIN表示选择行,只要它们存在于一个表中。

你需要FULL OUTER JOIN...

于 2012-05-08T02:48:47.037 回答