我有一个包含以下数据的表
taskid bind_address type_id
1 tcp://10.10.1.2:8001 3
1 tcp://127.0.0.1:8000 2
2 tcp://10.10.1.2:8003 3
2 tcp://127.0.0.1:8002 2
3 tcp://10.10.1.2:8005 3
3 tcp://127.0.0.1:8004 2
4 tcp://10.10.1.3:8007 3
4 tcp://127.0.0.1:8006 2
5 tcp://10.10.1.4:8009 3
5 tcp://127.0.0.1:8008 2
6 tcp://10.10.1.4:8011 3
6 tcp://127.0.0.1:8010 2
7 tcp://10.10.1.5:8011 3
7 tcp://127.0.0.1:8012 2
与此sqlStatement
select task_id, bind_address,type_id
From task_pub tp
join task t
on tp.task_id=t.id
join host h
on t.host_id=h.id
where h.id=3
group by h.id, task_id
having min(type_id);
对于每个任务,我只想获取具有最低type_id的行。如果我使用sqlite3执行此语句,则会得到此结果
2 tcp://127.0.0.1:8002 2
5 tcp://127.0.0.1:8008 2
6 tcp://127.0.0.1:8010 2
但是使用java和sqlite的JDBC驱动程序我得到这个(错误的)结果
2 tcp://10.10.1.2:8003 3
5 tcp://10.10.1.4:8009 3
6 tcp://10.10.1.4:8011 3
Java中的sql语句是100%正确的,我以调试模式复制了该语句
having min(type_id)
简直等同于:
having min(type_id) <> 0
大概,您需要type_id
每个的最小值task_id
。这是一种方法:
select task_id, bind_address, type_id
From task_pub tp join
task t
on tp.task_id = t.id join
host h
on t.host_id = h.id
where h.id = 3 and
not exists (select 1
from task_pub tp2
where tp2.task_id = tp.task_id and
tp2.host_id = tp.host_id and
tp2.type_id < tp.task_id
) ;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句