通过连接获得额外的行

时间:2014-10-10 09:59:55

标签: sql sql-server tsql

假设我有以下表格

CHAPTERS
| ID | Title        |
---------------------
|  1 | Introduction |

LINES
| ID | Chapter | Line                |
--------------------------------------
|  2 |       1 | Fourscore and ...   |
|  5 |       1 | In the beginning... |

我在SQL结果中寻找的是以下

| Title        | Line       |
-----------------------------
| Introduction | null       |
| Introduction | Fourscore  |
| Introduction | In the beg |

所以基本上我想要一个额外的行,只有标题和其他行与te匹配的行。 我现在所拥有的只是2行,其中没有缺少空行的行只有标题。

有什么想法吗?

1 个答案:

答案 0 :(得分:3)

试试这个

select title,null as Line from chapters
union
select title,line from chapters a join lines b on a.id=b.chapter
相关问题