2013-11-22 72 views
0

問題:我知道我需要使用2個表格。我需要使用哪個加入?

我需要找到書名,雜誌,傳單,在標題中有「運動」一詞。

Example Table Library1: books, magazines etc,... title 
Example Table Library2: flyers, etc (THIS TABLE DOES NOT HAVE title column anywhere) 

Psuedocode:從兩個表中選擇書籍,雜誌,傳單,其中標題爲'%sport%'; 我需要匹配「sport」的任何結果,並顯示它是否爲書籍,雜誌或其他內容。

不知道我是否需要左連接,連接或外部。

這是什麼語法?

+0

**此表沒有標題列隨處可見**這會有問題。你不能'UNION',因爲'UNION'必須在每個'UNION'中有相同的列,如果在表2中你沒有標題,你用什麼字段來搜索'SPORT'? – logixologist

回答

3

也沒有。你會使用聯盟。例如:

select title, 'book' as product from books where title like '%sport%' 
union all 
select title, 'magazine' from magazines where title like '%sport%' 
union all 
select title, 'flyer' from flyers where title like '%sport%' 
相關問題