
[英]Creating multiple SQL tables that have the same initial values but different table names when the query executes
[英]Retrieve the Table Names in a Query that Searches multiple tables
如何修改以下查询代码以在结果中包含表名?
$ query搜索数据库中的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";
}
$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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.