2012-11-18 117 views
-1

我有兩個不同的表,稱爲像新聞SpotNews。而這些表有像ID兩個相同的列,News_Title和News_Content ..有關的問題;我怎樣才能在同一個查詢中顯示SQL結果?如何在兩個不同的表中顯示相同的列項目?

例如(輸出應該是這樣的);

id  News_title  News_Content 
==================================== 
23  Title abc  content 123  // Comes from News 
67  Title ahs  content 233  // Comes from Spot_News 
223  Title abc4  content 321  // Comes From Spot_News 
367  Title ahseq  content tg3  // Comes from News 
567  Title ahs2  content 2da  // Comes from News 

你能寫一個sql查詢嗎? 謝謝。

+0

您如何知道要顯示哪些數據?什麼是標準? –

+0

@火箭我只是想在這些表格中顯示新聞標題和新聞內容欄(他們有同一列) –

+0

所以,他們之間沒有聯繫? –

回答

3

嘗試UNION

SELECT id, news_title, news_content FROM table_one 
UNION ALL 
SELECT id, news_title, news_content FROM table_two 
+0

非常感謝你;)我只是跟你學習UNION(:有很好的一個。 –

2

您正在尋找的UNION操作。

SELECT id, News_title, News_content FROM News WHERE 1 = 1 
UNION ALL 
SELECT id, News_title, News_content FROM Spot_News 

UNION允許您從兩個查詢整理結果,只要它們有相同數量的字段。每個查詢也可以有一組WHERE條款!

0

那麼,你只需要創建多個查詢。

$query = "SELECT news from TABLE_ONE WHERE ..."; 
$query2 = "SELECT news from TABLE_ONE WHERE ..."; 

$result = mysql_query($query); 
$result2 = mysql_query($query2); 

$allnews = $result.$result2; 

$while($row=mysql_fetch_array($allnews)){echo "$row[anything you want to echo here]";} 

我還沒有測試過這段代碼,所以試試吧。

+0

你不能用'.'運算符連接結果 – ariefbayu

相關問題