douti9253 2010-04-29 02:48
浏览 36

CodeIgniter中的MySQL Query问题

So I'm using the following:

    $r = new Record();
    $r->select('ip, count(*) as ipcount');
    $r->group_by('ip');
    $r->order_by('ipcount', 'desc');
    $r->limit(5);

    $r->get();

    foreach($r->all as $record)
    {
        echo($record->ip." ");
        echo($record->ipcount." <br />");
    }

Standard:

SELECT `ip`, count(*) as ipcount FROM (`soapi`) GROUP BY `ip` ORDER BY `ipcount` desc LIMIT 5;

And I only get the last (fifth) record echo'ed out and no ipcount echoed.

Is there a different way to go around this? I'm working on learning DataMapper (hence the questions) and need to figure some of this out. I haven't quite wrapped my head around the whole ORM thing.

Is there a way to set the count(*) as ipcount without the funny select() statement? I don't think it's triggering for some reason. This could also be a bug in DataMapper, but I'm less certain of that.

Also I found that even if I use the $r->query() method it doesn't return anything except the last entry if I use something like SELECTipFROMsoapiWHERE 1;. It will however return everything (like it should) if I say SELECT * FROM soapi WHERE 1;. If it doesn't have the * it only returns the last line.

Just verified with the new query, anything except selecting all columns (*) only returns the last record. Any help with this would be great. You can craft a statement like select *, count(*) as ipcount but then you still don't have access to it via $record->ipcount.

  • 写回答

1条回答 默认 最新

  • douxin8749 2011-11-25 07:19
    关注

    For your case, once you use COUNT() function in MySQL, it will only return 1 value. Hence you ip result data would not display out.

    I suggest you just split this 2 queries. 1. for COUNT(*) 2. for ip

    Hope this help.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集