SQL Tip: Display Bogus or Fake Column in SQL

So here is the issue i was having.

I have a Union All select statement. I want to display both blogs posts and content posts in the same dataset. The problem is that these are separated into different tables.

When I’m using the dataset how do I know where the row came from? Was it tbl_Content or tbl_Blog? I need to know this if I want to point the user to the right page.

Here is the answer.

[codesyntax lang=”sql”]
SELECT TOP 250 *
FROM
(SELECT tbl_Post.POST_ID, tbl_Post.POST_MESSAGE, tbl_Post.DATE_CREATED, tbl_Blog_Post.BLOG_ID AS ID, 'blog' AS coltype
FROM tbl_Post INNER JOIN
tbl_Blog_Post ON tbl_Post.POST_ID = tbl_Blog_Post.POST_ID
UNION ALL
SELECT tbl_Post.POST_ID, tbl_Post.POST_MESSAGE, tbl_Post.DATE_CREATED, tbl_Content_Post.CONTENT_ID AS ID,'content' AS coltype
FROM tbl_Post INNER JOIN
tbl_Content_Post ON tbl_Post.POST_ID = tbl_Content_Post.POST_ID) AS PostView

ORDER BY DATE_CREATED DESC
[/codesyntax]

I created a fake column called ‘coltype’ and set it to display blog or content in the returning Union All statement.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: