2012-12-16 105 views
0

如何修改以下查詢代碼以將表名包含在結果中?在查詢多個表的查詢中檢索表名稱

$查詢搜索數據庫中的4個表,我希望結果包括他們來自哪個表。所有4個表格都有相同的字段。

然後我想輸出結果如下面$ results_array []數組中所示。

在此先感謝。

$query = mysql_query("select * from $TableName_1 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
(convert(`Post_Title` using utf8) like '%$term_2%') OR 
(convert(`Post_Title` using utf8) like '%$term_3%') OR 
(convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select * from $TableName_2 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
(convert(`Post_Title` using utf8) like '%$term_2%') OR 
(convert(`Post_Title` using utf8) like '%$term_3%') OR 
(convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select * from $TableName_3 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
(convert(`Post_Title` using utf8) like '%$term_2%') OR 
(convert(`Post_Title` using utf8) like '%$term_3%') OR 
(convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select * from $TableName_4 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
(convert(`Post_Title` using utf8) like '%$term_2%') OR 
(convert(`Post_Title` using utf8) like '%$term_3%') OR 
(convert(`Post_Title` using utf8) like '%$term_4%')) 
order by Post_Date desc LIMIT $items_to_query"); 

while($row = mysql_fetch_assoc($query)){ 
$table_name = ?????????????????????; 
$db1 = $row['Post_Date']); 
$db2 = $row['Post_Title']; 
$db3 = $row['Author']); 

$results_array[] = '<div>'.$table_name.' - '.$db1.' - '.$db2.' - '.$db3.'</div>'."\n"; 
} 

回答

-1
$query = mysql_query("select *, '$TableName_1' as table_name from $TableName_1 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
(convert(`Post_Title` using utf8) like '%$term_2%') OR 
(convert(`Post_Title` using utf8) like '%$term_3%') OR 
(convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select *, '$TableName_2' as table_name from $TableName_2 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
(convert(`Post_Title` using utf8) like '%$term_2%') OR 
(convert(`Post_Title` using utf8) like '%$term_3%') OR 
(convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select *, '$TableName_3' as table_name from $TableName_3 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
(convert(`Post_Title` using utf8) like '%$term_2%') OR 
(convert(`Post_Title` using utf8) like '%$term_3%') OR 
(convert(`Post_Title` using utf8) like '%$term_4%')) 
UNION select *, '$TableName_4' as table_name from $TableName_4 where Topic = '$topic' AND 
((convert(`Post_Title` using utf8) like '%$term_1%') OR 
(convert(`Post_Title` using utf8) like '%$term_2%') OR 
(convert(`Post_Title` using utf8) like '%$term_3%') OR 
(convert(`Post_Title` using utf8) like '%$term_4%')) 
order by Post_Date desc LIMIT $items_to_query"); 
+0

謝謝你,但我怎麼會得到表名了在$ results_array []? – Sammy

+0

我剛剛用引號編輯它以獲取$ tableName var作爲字符串而不是表格。使用$ row ['table_name']; –

+0

我試過了,但沒有檢索到表名。我添加了$ table = $ row ['table_name'];在while循環和$ table到$ results_array []但我只有空白數據。 – Sammy