0

标题有点令人困惑,希望从我的查询中清楚我想要做什么。我以为我理解联接,显然不是,这是我的 $query:

     SELECT DATE(T0.timestamp),
            SUM(T0.total_responses), 
            SUM(T0.responses), 
            T0.metric_id, 
            T1.metric_id
       FROM `personal_aggregates` AS T0
  LEFT JOIN `qrs_metrics` AS T1 
         ON T0.metric_id = T1.qrs_metric_id
      WHERE T0.user_id = 1 AND 
            T0.duration = '1' AND 
            T0.category_id IN (1,2,3,4) AND 
            T0.timestamp >= 'period_duration'
   GROUP BY DATE(T0.timestamp)";

我正在尝试加入 metric_id 和 qrs_metric_id 列上的表。但是我没有从上述查询中得到任何结果。当我在 PHP 中遍历结果并检查任何变量时,例如$result['T0.metric_id'],我得到Undefined index: T0.metric_id

如果有人能对此有所了解,我将不胜感激。

4

3 回答 3

0

您的查询应该是

$query = "SELECT 
                DATE(T0.timestamp) AS T0timestamp, 
                SUM(T0.total_responses) AS total_responses, 
                SUM(T0.responses) AS responses,
                T0.metric_id, 
                T1.metric_id AS metric_id_2
        FROM `personal_aggregates` AS T0

并像访问

$result['metric_id'];
$result['metric_id_2'];
$result['T0timestamp'];
$result['total_responses'];

从关联数组访问中排除表别名,例如“T0.XXX”是错误的。您只能在 SQL 中执行此操作

于 2013-07-05T15:05:22.147 回答
0

试试这个:

$query = "SELECT DATE(T0.timestamp), SUM(T0.total_responses), SUM(T0.responses),
               T0.metric_id AS metric0, T1.metric_id AS metric1
        FROM `personal_aggregates` AS T0
        LEFT JOIN `qrs_metrics` AS T1 ON T0.metric_id = T1.qrs_metric_id
        WHERE T0.user_id = 1 AND T0.duration = '1'
        AND T0.category_id IN (1,2,3,4)
        AND T0.timestamp >= 'period_duration'
        GROUP BY DATE(T0.timestamp)";

您现在可以使用以下方法有所作为:

echo $row['metric0'];
echo $row['metric1'];
于 2013-07-05T15:06:02.490 回答
0

您需要为所选列添加一些别名

SELECT DATE(T0.timestamp) as tstamp, SUM(T0.total_responses) as responses...

并按别名引用列:

$result['tstamp']

或者您可以按数字索引引用列:

$result[0]
于 2013-07-05T15:07:51.267 回答