doz59484 2014-11-03 23:02
浏览 45
已采纳

将数据附加到num查询

I'm using the following query to display pages whose URL's are stored in two different tables. (Actually, I'll soon have four or five joined tables.)

$sql = "SELECT SUM(num) as num FROM (
 SELECT COUNT(URL) AS num FROM gw_geog WHERE URL = :MyURL
 UNION ALL
 SELECT COUNT(URL) AS num FROM gw_world_urls WHERE URL = :MyURL
 ) AS X";
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();

Of course, this query only returns 0, 1, 2, etc. I just wondered if it's possible to extract additional information from such a query.

For example, is it possible to modify it so that every page derived from table gw_geog has the value $MyKind = 'This', while every page derived from table gw_world_urls has the value $MyKind = 'That'?

Or suppose table gw_world has a field named Rank with various values, like 1, 2, 3. Is it possible to say that a URL derived from that table (e.g. arizona) has a rank of 2, where the row values are arizona | 2?

  • 写回答

1条回答 默认 最新

  • dongyu9667 2014-11-03 23:53
    关注

    Im not sure I understand what you want. I think you want to count how many rows you have in x tables where URL field = something. Then you want to know if a specific row of these tables (e.g. the one from gw_world where url = arizona) have a specific value for rank.

    Do you really need this in only one query? Because you will get many repeated "sum" fields.

    You could make this query to count the results and another one to get all rows with their respective ranks.

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

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探