select NEW_YGINFO.YGID,NEW_DEPT.DEPTNAME,NEW_YGINFO.YGNAME,NEW_DEPTGW.DEPT_GW,NEW_YGINFO.YGSFZ,NEW_YGINFO.LOGINNAME,NEW_YGINFO.ROLE,NEW_YGINFO.PASSWORD,count(*) from new_yginfo inner join new_dept on new_yginfo.deptid = new_dept.id inner join new_deptgw on NEW_DEPTGW.DEPT_GWID = NEW_DEPT.ID查询结果如下:ygid deptname ygname dept_gw ygsfz loginname role password
88 技术装备科 神啊 副科长 133131231231 神啊 1111
88 技术装备科 神啊 科长 133131231231 神啊 1111
88 技术装备科 神啊 科员 133131231231 神啊 1111
281 技术装备科 掌握 副科长 掌握 0 1111
281 技术装备科 掌握 科长 掌握 0 1111
281 技术装备科 掌握 科员 掌握 0 1111
282 技术装备科 地方 副科长 地方 0 1111
282 技术装备科 地方 科长 地方 0 1111
282 技术装备科 地方 科员 地方 0 1111

解决方案 »

  1.   

    这样让人如何理解?我给你个例,自己参考修改.select a.* , t.* from a, b t
    where a.id = t.id and t.val = (select max(val) from b where id = t.id)select a.* , t.* from a, b t
    where a.id = t.id and not exists (select 1 from b where id = t.id and val > t.val)
      

  2.   


    inner join new_deptgw 
    on NEW_DEPTGW.DEPT_GWID = NEW_DEPT.ID
    这部分关联关系有问题,应当是在表new_deptgw中同一科室存在3条记录,分别是科长、副科长、科员,你只用id进行关联就导致结果集中的记录数变成3倍。去除这个关联或者完善这个关联都可以解决这个问题。
      

  3.   

    在select后面第一个字段前加上distinct即可。
      

  4.   

    select DISTINCT NEW_YGINFO.YGID,NEW_DEPT.DEPTNAME,NEW_YGINFO.YGNAME,NEW_DEPTGW.DEPT_GW,NEW_YGINFO.YGSFZ,NEW_YGINFO.LOGINNAME,NEW_YGINFO.ROLE,NEW_YGINFO.PASSWORD,count(*) from new_yginfo inner join new_dept on new_yginfo.deptid = new_dept.id inner join new_deptgw on NEW_DEPTGW.DEPT_GWID = NEW_DEPT.ID
      

  5.   


    select 
    NEW_YGINFO.YGID,
    NEW_DEPT.DEPTNAME,
    NEW_YGINFO.YGNAME,
    NEW_DEPTGW.DEPT_GW,
    NEW_YGINFO.YGSFZ,
    NEW_YGINFO.LOGINNAME,
    NEW_YGINFO.ROLE,
    NEW_YGINFO.PASSWORD,
    count(*)from new_yginfo , new_dept, NEW_DEPTGW
    where  new_depton new_yginfo.deptid = new_dept.id 
    and 
    new_deptgw on NEW_DEPTGW.DEPT_GWID = NEW_DEPT.ID
    这样写就正确了~