以下查询只是来自三个不同网站的查询集合,这些网站链接在一起,形成第四个网站中的单个查询。
它工作正常,但我需要弄清楚如何维护每个子查询的身份。例如,如果脚本显示表calendar_gw中的一些数据,我想知道该数据最初与网站GeoWorld(gw)相关联。然后我可以修改内容/显示或只是包含一个注释“来自GeoWorld的数据。”
我认为显而易见的解决方案是简单地使用别名,就像这样......
CGZ.Brief AS GZ_Brief
然后我可以进入while循环并插入类似以下内容之一......
$Brief = $row['GZ_Brief];
$Brief = $row['CGZ.GZ_Brief];
$Brief = $row['CGZ_Brief];
但那些不适合我。另一个可能更好的解决方案是创建一个PHP交换机,以某种方式将网站与输出相关联......
switch($Brief)
{
case 'CGZ_Brief':
$MySiteID = 'GW';
break;
default:
break;
}
谁能告诉我如何完成我想要做的事情? (顺便说一句,价值$ MyURL只是一个页面网址,如网址MySite / Calendar / May_1中的May_1。)
$stm = $pdo->prepare("SELECT CGW.N, CGW.URL, CGW.Date, CGW.Year, CGW.Brief
FROM calendar_gw CGW
WHERE CGW.URL = :MyURL
UNION ALL
SELECT CGZ.N, CGZ.URL, CGZ.Date, CGZ.Year, CGZ.Brief
FROM calendar_gz CGZ
WHERE CGZ.URL = :MyURL
UNION ALL
SELECT CSL.N, CSL.URL, CSL.Date, CSL.Year, CSL.Brief
FROM calendar_sl CSL
WHERE CSL.URL = :MyURL
ORDER BY Year");
$stm->execute(array(
'MyURL'=>$MyURL
));
$prevYear = null;
while ($row = $stm->fetch())
{
$Brief2 = $row['Brief'];
$Year = $row['Year'];
if ($Year == $prevYear) {
$YearStr = '• ';
} else {
$YearStr = $Year . '– ';
$prevYear = $Year;
}
$Brief[] = $YearStr.$row['Brief'];
}
答案 0 :(得分:1)
为什么不添加静态值?
$stm = $pdo->prepare("SELECT 'CGW' AS Site, CGW.N, CGW.URL, CGW.Date, CGW.Year, CGW.Brief
FROM calendar_gw CGW
WHERE CGW.URL = :MyURL
UNION ALL
SELECT 'CGZ' AS Site, CGZ.N, CGZ.URL, CGZ.Date, CGZ.Year, CGZ.Brief
FROM calendar_gz CGZ
WHERE CGZ.URL = :MyURL
UNION ALL
SELECT 'CSL' AS Site, CSL.N, CSL.URL, CSL.Date, CSL.Year, CSL.Brief
FROM calendar_sl CSL
WHERE CSL.URL = :MyURL
ORDER BY Year");