Matt Stewart
Matt Stewart

Reputation: 322

SQL Having a single column display value from different tables

I need some help. I'm trying to display a field called SubmissionStatus in a query. However I need to select it from multiple tables. For example...

I have:

Table0

Table1.SubmissionStatus Table2.SubmissionStatus Table3.SubmissionStatus (All three of these tables have a foreign key to relate them to Table0)

I can do: SELECT Table1.SubmissionStatus, Table2.SubmissionStatus, Table3.SubmissionStatus FROM Table0

But that won't give me anything

I need something like: SELECT SubmissionStatus FROM Table0 (As one column)

Help please?

Upvotes: 0

Views: 2580

Answers (1)

Kreg
Kreg

Reputation: 647

I would suggest using a union, like so:

SELECT T1.SubmissionStatus 
FROM Table1 T1
JOIN Table0 T0 ON T1.primaryKey = T0.foreignKey
UNION
SELECT T2.SubmissionStatus 
FROM Table1 T2
JOIN Table0 T0 ON T2.primaryKey = T0.foreignKey
UNION
SELECT T3.SubmissionStatus 
FROM Table1 T3
JOIN Table0 T0 ON T3.primaryKey = T0.foreignKey

That will put everything in one column (think of a union kind of like the JOIN keyword, but for joining rows instead of columns).

Upvotes: 3

Related Questions