简体   繁体   English

CodeIgniter中的MySQL查询问题

[英]MySQL Query problem in CodeIgniter

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. 而且我只得到回显的最后一个(第五个)记录, 没有回显任何 ipcount

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. 我正在学习DataMapper(因此有问题),需要弄清楚其中的一些内容。 I haven't quite wrapped my head around the whole ORM thing. 我还没有完全把头放在整个ORM上。

Is there a way to set the count(*) as ipcount without the funny select() statement? 有没有一种方法可以将count(*) as ipcount而不使用有趣的select()语句? 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. 这也可能是DataMapper中的错误,但我对此不太确定。

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 SELECT ip FROM soapi WHERE 1; 我还发现,即使我使用$r->query()方法,如果我使用SELECT ip FROM soapi WHERE 1;类的东西,它也不会返回除最后一项以外的任何内容WHERE 1; . It will however return everything (like it should) if I say SELECT * FROM soapi WHERE 1; 但是,如果我说SELECT * FROM soapi WHERE 1; ,它将返回所有内容(应该这样) 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 . 您可以select *, count(*) as ipcount一条语句,例如select *, count(*) as ipcount但是仍然无法通过$record->ipcount访问它。

For your case, once you use COUNT() function in MySQL, it will only return 1 value. 对于您的情况,一旦在MySQL中使用COUNT()函数,它将仅返回1值。 Hence you ip result data would not display out. 因此,您的ip结果数据将不会显示出来。

I suggest you just split this 2 queries. 我建议您只拆分这两个查询。 1. for COUNT(*) 2. for ip 1.代表COUNT(*)2.代表ip

Hope this help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM