Davio
Davio

Reputation: 4737

Get an extra row with a join

Assume I have the following tables

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

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

What I'm looking for in my SQL result is the following

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

So basically I want an extra row with just the Title and other rows with te matching lines. All I've got now is just the 2 rows with the lines without the missing null-line with just the title.

Any ideas?

Upvotes: 0

Views: 38

Answers (1)

Recursive
Recursive

Reputation: 952

Try this

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

Upvotes: 3

Related Questions