使用 GROUP_CONCAT 查询不适用于 PHP [英] Query with GROUP_CONCAT not working with PHP

查看:23
本文介绍了使用 GROUP_CONCAT 查询不适用于 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 GROUP_CONCAT 和 GROUP BY 显示某些值,但在我测试时只显示组,而其他值不显示.

I am trying to display certain values through GROUP_CONCAT, and GROUP BY, yet only the groups are showing when I test it out, and the other values don't.

function events_calendar() {
     global $connection;
     mysqli_select_db($connection);
     $query = ("SELECT month, GROUP_CONCAT(start_date) as data FROM events GROUP BY month");
     $result = $connection->query($query);
     $str = "";
     while ($row = $result->fetch_assoc()) {
         echo $row['month'];
         echo $row['start_date'];
     }
     return $str;
}

月份值显示,但 start_date 值不显示.

The month values show up, but the start_date values don't.

推荐答案

问题在于 PHP.尝试更换:

The problem is the PHP. Try replacing:

     echo $row['start_date'];

与:

     echo $row['data'];

您需要使用您在 SELECT 中指定的列别名.

You need to use the column alias that you assigned in the SELECT.

这篇关于使用 GROUP_CONCAT 查询不适用于 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆