douliang1891 2014-08-03 18:43
浏览 42
已采纳

SQL:我可以在GROUP BY查询中获取别名吗?

I have this query:

SELECT date, sum(money) as total
    FROM gasto 
    GROUP BY date;

The table in question has two fields: date (where the input is a specific day), and money (an integer).

To print out the sums, for each day, I do this:

$resultado=mysqli_query($conectar,$consulta); //this is to get the result
foreach ($resultado as $key => $value) {
    echo $key.' - '.$value['total'].'<br>';

This results in:

0 - 20

1 - 45

2 - 12

etc.

My question is, how do I print out each date right before the aumont?

  • 写回答

2条回答 默认 最新

  • dongyu4908 2014-08-03 18:52
    关注

    Try this:

    $resultado=mysqli_query($conectar,$consulta); //this is to get the result
    foreach ($resultado as $key => $value) {
        echo $key.' - '. $value['date'] . ' : ' . $value['total'].'<br>';    
    }
    

    Your selected table names will not change with group by.

    You also can do this if you want to give an alias:

    //SELECT date as mydate, sum(money) as total FROM gasto GROUP BY date;
    $resultado=mysqli_query($conectar,$consulta); //this is to get the result
    foreach ($resultado as $key => $value) {
        echo $key.' - '. $value['mydate'] . ' : ' . $value['total'].'<br>';    
    }
    

    For the future you can do print_r($resultado) to see, what you get from your database fetch method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度