我正在从任何mysql客户端执行此查询,并且它正在运行:
SELECT
q.calldate,
q.dst,
@ts_dnd_activate := IF(q.dst='*78',q.calldate,@ts_dnd_activate) AS dndact,
@ts_dnd_deactivate := IF(q.dst='*79',q.calldate,NULL) AS dnddeact,
TIMESTAMPDIFF(SECOND,@ts_dnd_activate,@ts_dnd_deactivate) AS tdiff
FROM
(SELECT calldate,dst
FROM cdr
WHERE calldate>='2012-01-09 11:12:08' AND src=1004 AND dst IN ('*78','*79')
ORDER BY calldate DESC) q
它从表中获取两个字段,并根据dst字段的值,使用mysql变量计算calldates字段之间的差异,这是一个时间戳。
如果我在PHP代码中运行此查询:
[...]
$q = "SELECT
q.calldate,
q.dst,
@ts_dnd_activate := IF(q.dst='*78',q.calldate,@ts_dnd_activate) AS dndact,
@ts_dnd_deactivate := IF(q.dst='*79',q.calldate,NULL) AS dnddeact,
TIMESTAMPDIFF(SECOND,@ts_dnd_activate,@ts_dnd_deactivate) AS tdiff
FROM
(SELECT calldate,dst
FROM cdr
WHERE calldate>='2012-01-09 11:12:08' AND src=1004 AND dst IN ('*78','*79')
ORDER BY calldate DESC) q";
$rs = mysql_query($q,$database);
[...]
我永远不会得到tdiff字段,因为我得到的行mysql变量总是为NULL,如下所示:
Array
(
[calldate] => 2012-01-09 12:20:10
[dst] => *78
[dndact] => 2012-01-09 12:20:10
[dnddeact] =>
[tdiff] =>
)
Array
(
[calldate] => 2012-01-09 12:22:05
[dst] => *79
[dndact] =>
[dnddeact] => 2012-01-09 12:22:05
[tdiff] =>
)
Array
(
[calldate] => 2012-01-09 12:25:15
[dst] => *78
[dndact] => 2012-01-09 12:25:15
[dnddeact] =>
[tdiff] =>
)
Array
(
[calldate] => 2012-01-09 12:29:21
[dst] => *79
[dndact] =>
[dnddeact] => 2012-01-09 12:29:21
[tdiff] =>
)
请注意,它是一个单一的查询,但似乎不可能在php mysql基本扩展中使用mysql变量,因为我不知道为什么但似乎每行都设置为null。
我做错了什么?这是一个新问题吗?
P.S。请不要告诉我我要使用PDO或mysqli;这是一个我无法重写的旧项目。