2019-07-18 15:02:00 1560次浏览 0条回答 0 悬赏 20 金钱

今天做到数据统计,不想用拼接sql,用模型来查遇到了个问题
查询1分钟内访问大于60的数据:

$q1 = Weblogs::find()
    ->where(['website'=>$domain])
    ->andWhere(['between','`time`',$stime,$etime])
    ->groupBy("`ip`,DATE_FORMAT(`time`,'%Y-%m-%d %H:%i')")
    ->select("count(ip) as num,ip,DATE_FORMAT(`time`,'%Y-%m-%d %H:%i') as time");

$query = new Query();
$list = $query->select('*')->from(['t1' => $q1])->where('num > 60')->createCommand()->queryAll();

结果数据库报错:sql为:
SELECT * FROM (SELECT count(ip) AS num, ip, DATE_FORMAT(time, '%Y-%m-%d %H:%i') AS time FROM rf_weblogs WHERE (website='www.inihouse.com') AND (time BETWEEN '2019-07-18 00:00:00' AND '2019-07-18 23:59:59') GROUP BY ip, DATE_FORMAT(time, '%Y-%m-%d %H:%i')) t1 WHERE num > 60
在 DATE_FORMAT(time, '%Y-%m-%d %H:%i') 中多了一对 `` 这怎么处理

补充于 2019-07-18 15:05

微信图片_20190718150524.png

补充于 2019-07-18 15:13

解决了,传数组就可以了

$q1 = Weblogs::find()
->where(['website'=>$domain])
->andWhere(['between','`time`',$stime,$etime])
->groupBy(['ip',"DATE_FORMAT(`time`,'%Y-%m-%d %H:%i')"])
->select(['count(ip) as num','ip',"DATE_FORMAT(`time`,'%Y-%m-%d %H:%i')"]);

$query = new Query();
$list = $query->select('*')->from(['t1' => $q1])->where('num > 60')->createCommand()->queryAll();
    没有找到数据。
您需要登录后才可以回答。登录 | 立即注册
nswindlights
总监

nswindlights 江苏太仓

注册时间:2014-09-18
最后登录:2020-03-30
在线时长:74小时58分
  • 粉丝5
  • 金钱986
  • 威望80
  • 积分2526

热门问题