Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 选择在指定列中具有最高值且在其他列中不具有唯一值的行应是不同的_Mysql_Sql_Kohana_Kohana Orm - Fatal编程技术网

Mysql 选择在指定列中具有最高值且在其他列中不具有唯一值的行应是不同的

Mysql 选择在指定列中具有最高值且在其他列中不具有唯一值的行应是不同的,mysql,sql,kohana,kohana-orm,Mysql,Sql,Kohana,Kohana Orm,我有一张桌子活动: 我必须选择一行中的所有ip和其他数据,但仅在最后一个活动的位置是最高的。所以结果应该是这样的: 3 3000 user3 192.168.10.30 6 6000 user2 192.168.10.20 7 7000 user1 192.168.10.10 我尝试过使用以下方法: ORM::factory('Activity')->order_by('last_active','DESC')->group_by('ip')->find

我有一张桌子活动:

我必须选择一行中的所有ip和其他数据,但仅在最后一个活动的位置是最高的。所以结果应该是这样的:

3 3000 user3 192.168.10.30 6 6000 user2 192.168.10.20 7 7000 user1 192.168.10.10 我尝试过使用以下方法:

ORM::factory('Activity')->order_by('last_active','DESC')->group_by('ip')->find_all(); 结果是:

array(4) ( "id" => string(1) "3" "last_active" => string(4) "3000" "login" => string(5) "user3" "ip" => string(13) "192.168.10.30" ) array(4) ( "id" => string(1) "2" "last_active" => string(4) "2000" "login" => string(5) "user2" "ip" => string(13) "192.168.10.20" ) array(4) ( "id" => string(1) "1" "last_active" => string(4) "1000" "login" => string(5) "user1" "ip" => string(13) "192.168.10.10" ) 如您所见,我有不同的IP,但最后一个活动值不正确。 有什么想法吗?纯SQL语句或DB::select回答是可以的:-

怎么样

select id,  last_active, login,   ip
from activities
where (last_active, ip) in (select max(last_active), ip from activities group by ip )
使用。类似于以下的方法应该可以工作:

ORM::factory('Activity')
    ->where(
        DB::expr(
            '`last_active`,`ip`'
        ),
        'IN',
        DB::expr(
            'select max(`last_active`), `ip` from `activities` group by `ip`'
        )
    ->find_all();

我可以给出纯sql语句,但不确定如何在ORMWorks中实现它!非常感谢。现在我必须弄清楚如何将其转换为Kohana ORM或DB查询生成器:在你的问题中,你说纯SQL是可以的。如果您在ORM中需要它,我无法帮助您:/
ORM::factory('Activity')
    ->where(
        DB::expr(
            '`last_active`,`ip`'
        ),
        'IN',
        DB::expr(
            'select max(`last_active`), `ip` from `activities` group by `ip`'
        )
    ->find_all();