标题有点令人困惑,希望很明显我想从我的查询中做些什么。我以为我理解了连接,显然不是,这是我的$查询:
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 metric和qrs_metric_id上加入表。但是我从上面的查询中得不到任何结果。当我在PHP中遍历结果并检查任何变量时,例如$result['T0.metric_id']
,我得到Undefined index: T0.metric_id
如果有人能对此有所了解,我将非常感激。
答案 0 :(得分: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
中执行此操作答案 1 :(得分: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'];
答案 2 :(得分:0)
您需要在所选列中添加一些别名
SELECT DATE(T0.timestamp) as tstamp, SUM(T0.total_responses) as responses...
并按别名引用列:
$result['tstamp']
或者您可以通过数字索引引用列:
$result[0]