Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/59.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 在同一查询中选择DISTINCT和MAX,并列出行中的所有值_Mysql_Sql_Max_Distinct - Fatal编程技术网

Mysql 在同一查询中选择DISTINCT和MAX,并列出行中的所有值

Mysql 在同一查询中选择DISTINCT和MAX,并列出行中的所有值,mysql,sql,max,distinct,Mysql,Sql,Max,Distinct,我试图在下表中组合distinct和max assign\u id或last assign\u id,但没有得到正确的值 表2任务 ASSIGN_ID | DRV_ID | VEHICLE_ID -------------------------------------- 1 | EFFA | 1000 2 | SAM | 1001 3 | FIZA | 1004 4

我试图在下表中组合distinct和max assign\u id或last assign\u id,但没有得到正确的值

表2任务

ASSIGN_ID  |  DRV_ID   | VEHICLE_ID
-------------------------------------- 
    1      |    EFFA   |   1000  
    2      |    SAM    |   1001 
    3      |    FIZA   |   1004 
    4      |    JIJO   |   1000 
    5      |    LISA   |   1000  
如何获得如下所示的值显示

ASSIGN_ID   |  DRV_ID  |  VEHICLE_ID 
----------------------------------------- 
     2      |   SAM    |   1001  
     3      |   FIZA   |   1004 
     5      |   LISA   |   1000  

有一个子查询,返回每个车辆id的最大分配id。
JOIN
,结果如下:

select t1.*
from task t1
join (select vehicle_id, max(assign_id) assign_id
      from task
      group by vehicle_id) t2
  on t1.vehicle_id= t2.vehicle_id
  and t1.assign_id = t2.assign_id

假设
assign\u id
没有关系,则可以使用子查询:

select t.*
from table t
where assign_id = (select max(t1.assign_id)
                   from table t1
                   where t1.vehicle_id = t.vehicle_id
                   );

哪一个MySQL版本?您需要的最大值是多少?换句话说,您希望通过vehicle_id@Matteo Meil获得最近分配的drv_id,我正在按上述方式编辑表格..最有用..;-)。非常感谢。