sql 查询中 非主键不能相同的问题。帮忙解决非常感谢。
员工表结构:
id (唯一), name , organ(机构), branch(部门),ordernumber(订单)
001 张三 北京 财务 20
005 李四 北京 财务 42
007 赵六 上海 财务 56
订单大于10的,这三条都符合条件,但是要求查询结果中,同一个机构部门也不能重复。也就是张三、李四只能取其中一个,怎么去都成。例如结果是:
001 张三 北京 财务 20
007 赵六 上海 财务 56
求高手解决啊。谢谢了
[解决办法]
select t.* from tb t where ordernumber = 10 and id = (select min(id) from tb where organ = t.organ and branch = t.branch and ordernumber = 10) select t.* from tb t where ordernumber = 10 and id = (select max(id) from tb where organ = t.organ and branch = t.branch and ordernumber = 10) select t.* from tb t where ordernumber = 10 and not exists (select 1 from tb where organ = t.organ and branch = t.branch and ordernumber = 10 and id < t.id) select t.* from tb t where ordernumber = 10 and not exists (select 1 from tb where organ = t.organ and branch = t.branch and ordernumber = 10 and id > t.id)
[解决办法]
select * from tb t where ordernumber = 10 and id = (select max(id) from tb where organ=t.organ and branch=t.branch and ordernumber=10)
[解决办法]