MySQL在视图中组合多个表

时间:2013-02-25 17:15:32

标签: mysql view sql-view

我希望能够在视图中组合3个表:

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

所以基本上,我想将每个表的内容映射到我的单个结果视图,然后我可以查询它们:

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

所以我能做到:

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

这是否可以实现,我该如何解决?

1 个答案:

答案 0 :(得分:2)

试试这个

 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'

这将包含您视图中的所有记录