1
所以我有一個查詢,我從兩個不同的表中選擇值並將它們聯合在一起。我需要知道哪些條目來自哪裏,因爲我需要爲每個條目形成不同的網址。標記這兩個SELECT查詢的最佳方式和正確的語法是什麼?任何幫助,將不勝感激。代碼如下:PHP MySQL使用UNION查詢並查找數據來自哪個表
$query = "SELECT newsletter_id id, newstitle title FROM tbl_news WHERE (newstitle LIKE '%".stripslashes($rowCont->fname)."%' && newstitle LIKE '%".stripslashes($rowCont->lname)."%') || (newsletter_content LIKE '%".stripslashes($rowCont->fname)."%' && newsletter_content LIKE '%".stripslashes($rowCont->lname)."%') SET @type = news
UNION
SELECT event_id id, event_title title FROM tbl_event WHERE ((event_title LIKE '%".stripslashes($rowCont->fname)."%' && event_title LIKE '%".stripslashes($rowCont->lname)."%') || (event_content LIKE '%".stripslashes($rowCont->fname)."%' && event_content LIKE '%".stripslashes($rowCont->lname)."%')) SET @type = event";
$result = mysql_query($query) or die(mysql_error());
$i = 0;
while($row = mysql_fetch_assoc($result)) {
if($table_name == table1) { //SIMULATED TABLE NAME VARIABLE IF STATEMENT, NEED TO KNOW HOW TO GET TABLE NAME FOR SORTING
echo "<p><a style='color:#00aeef' href='http://www.url1.com/display.php?article_=".$row["id"]."'>".$row["title"]."</a></p>";
} else {
echo "<p><a style='color:#00aeef' href='http://www.url1.com/display.php?article_=".$row["id"]."'>".$row["title"]."</a></p>";
}
}
只是讓你知道**所有mysql_ *函數都被棄用的註釋...... **你可以使用mysqli或者pdo。 :) – 2013-02-27 16:55:55
@SilentByte我看到你的推理,但它會比它的價值更麻煩。在這種情況下它是一個單獨的實例。 – 2013-02-27 17:06:48