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.

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

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题