我有这个查询,它完全符合我想要的phpmyadmin,它返回我的网站所拥有的访问次数。例如:visitorCount:2204
set @last_uid = 0;
set @last_tm = '00:00';
set @last_vid = 0;
SELECT MAX(
CASE WHEN @last_uid = visitorid AND cast( @last_tm as datetime) >= subtime( timestamp, '01:00' )
THEN
if( (@last_tm := timestamp ), @last_vid, @last_vid)
ELSE
if( (@last_uid := visitorid) + (@last_vid := @last_vid+1) +(@last_tm := timestamp ),@last_vid, @last_vid )
END
) as visitorCount
FROM visits v
ORDER BY timestamp DESC, visitorid
当我尝试从PHP执行此查询但它无法正常工作时,我已经使用常规的php mysql连接和cakephp $ this-> Model-> query()进行了测试;没有任何成功,我从查询得到的只是“1”或真。怎么了?
$query = "set @last_uid = 0;
set @last_tm = '00:00';
set @last_vid = 0;
SELECT MAX(
CASE WHEN @last_uid = visitorid AND cast( @last_tm as datetime) >= subtime( timestamp, '01:00' )
THEN
if( (@last_tm := timestamp ), @last_vid, @last_vid)
ELSE
if( (@last_uid := visitorid) + (@last_vid := @last_vid+1) +(@last_tm := timestamp ),@last_vid, @last_vid )
END
)
FROM visits v
ORDER BY timestamp DESC, visitorid";
$con = mysql_connect("host", "user", "pass");
if (!$con) {
die(mysql_error());
}
mysql_select_db("dbname", $con);
$result = mysql_query($query);
print_r(mysql_fetch_row($result));
给我错误信息:
mysql_fetch_row() expects parameter 1 to be resource, boolean given
答案 0 :(得分:1)
我不确定是不是因为你正在执行多个语句,而是尝试将它组合成一个,
SELECT MAX(CASE WHEN @last_uid = visitorid AND cast( @last_tm as datetime) >= subtime( timestamp, '01:00' )
THEN if((@last_tm := timestamp), @last_vid, @last_vid)
ELSE if((@last_uid := visitorid) + (@last_vid := @last_vid+1) + (@last_tm := timestamp), @last_vid, @last_vid )
END)
FROM visits v,
(SELECT @last_uid := 0, @last_tm := '00:00', @last_vid := 0;) x
ORDER BY timestamp DESC, visitorid