douyou1937 2015-10-07 22:37
浏览 16
已采纳

可以从COUNT个查询中提取信息吗?

Is it possible to extract information from a count query?

I'm going to be querying a lof of very big database tables as part of an online encyclopedia. It tentatively looks like I'll have to make at least three major UNION ALL queries.

The first query is a COUNT query to determine if a URL matches a value in my database and a page should therefore be displayed. The second query would probably target the field URL on each table and look something like this:

SELECT URL, 'World' AS MySection, 'GW' AS MySiteID

This allows me to assign a website and section ID to each database table. The third query would then group tables with the same website and/or section ID together and query them for additional information.

Anyway, I wondered if I could eliminate the second query by assigning site and section ID's in my count query. I think the answer is NO, but I thought I read somewhere that it can be done. Can anyone tell me how to do it, or is this a lost cause? The way I'm doing it below doesn't work for obvious reasons.

$sql = "SELECT
 SUM(num) AS num
FROM
(
 SELECT COUNT(URL) AS num, 'World' AS MySection, 'GW' AS MySiteID, gw_geog.`URL` as URL  
 FROM gw_geog
 WHERE URL = :MyURL AND G1 = 1
 UNION ALL
 SELECT COUNT(URL) AS num, 'World' AS MySection, 'GW' AS MySiteID, gw_geog_political.`URL` as URL
 FROM gw_geog_political
 WHERE URL = :MyURL
) AS X
WHERE X.URL like  :MyURL
LIMIT 1";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();

while ($row = $stm->fetch())
{
 $MySiteID = $row['MySiteID'];
 $MySection = $row['MySection'];
}
  • 写回答

1条回答 默认 最新

  • dskzap8756 2015-10-07 22:47
    关注

    I would get rid of the count (its expensive as well). And just get the two fields. If they are null, then there is nothing to display.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加