Question

I have 3 tables that I want to be able to combine in a view:

tblContent:
strTitle        | txtContent
content 1 title | content 1 content
content 2 title | content 2 content
content 3 title | content 3 content 

tblNews
news_title      | news_content
news 1 title    | news 1 content
news 2 title    | news 2 content

tblTradeMembers
company_title   | company_content
trade 1 title   | trade 1 content
trade 2 title   | trade 2 content
trade 3 title   | trade 3 content
trade 4 title   | trade 4 content

So basically, I want to map the contents of each of these tables to my single result view which I am then able to query which will be like:

title           | content             | from
content 1 title | content 1 content   | tblContent
content 2 title | content 2 content   | tblContent
content 3 title | content 3 content   | tblContent
news 1 title    | news 1 content      | tblNews
news 2 title    | news 2 content      | tblNews
trade 1 title   | trade 1 content     | tblTradeMembers
trade 2 title   | trade 2 content     | tblTradeMembers
trade 3 title   | trade 3 content     | tblTradeMembers
trade 4 title   | trade 4 content     | tblTradeMembers

So I can do:

select * from custom_view WHERE content LIKE "%3%"

Is this achievable and how do I go about it?

Was it helpful?

Solution

Try this

 SELECT strTitle AS 'TITLE', txtContent AS 'Content', 'tblContent' AS 'From'
 UNION     
 SELECT news_title AS 'TITLE', news_content AS 'Content', 'tblNews' AS 'From'
 UNION
 SELECT company_title AS 'TITLE', company_content AS 'Content', 'tblTradeMembers' AS 'From'

This will club all the records in your view

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top