实用但 好几个人没解决的SQL 查询
select min(enddate) enddate from hm_em_contracts C where C.em_id= '111222 '
group by C.em_id
这个查询没问题,但我还想多显示ID 这个字段,我写成这样
select min(C.id)) ID ,min(enddate) enddate from hm_em_contracts C where C.em_id= '111222 '
group by C.em_id
语句通过,但这个ID不是对应相关记录的值。 如下
id enddate
5 2003-06-01
但应在数据库中的ID 是 7
怎 么解决?
[解决办法]
select min(C.id)) ID ,min(enddate) enddate from hm_em_contracts C where C.em_id= '111222 '
group by C.em_id
==================================================
select C.id ID ,min(enddate) enddate from hm_em_contracts C where C.em_id= '111222 '
group by C.em_id
改成这样就可以了
[解决办法]
select C.id ID ,min(enddate) enddate from hm_em_contracts C where C.em_id= '111222 '
group by C.em_id,C.id
[解决办法]
这样试试
update hm_em_contracts set serialNO=C.code+ '01 '
from
(select min(enddate) enddate,em_id
from hm_em_contracts C group by em_id) T
inner join hm_employees C on C.em_id=T.em_id and c.enddate=t.enddate
where hm_em_contracts.id=C.id
[解决办法]
d 前面 的括号里面没有 select 不是一个结果集,所以不能加别名
这样试试
update hm_em_contracts set serialNO= '01 '
from
(select C.em_id, min(enddate) enddate
from hm_em_contracts C group by C.em_id) b
inner join hm_em_contracts a
on a.em_id=b.em_id and a.enddate=b.enddate
where a.em_id is not null
不是要找 ID嘛? 最后一句也可以用 where a.id is not null