表staff
id    fenshu   xiuxi    bumen
1     80       0        2
2     80       1        2
3     90       2        2
4     60       0        3要求:1.同一部门(bumen)的只显示一条数据
      2.取值顺序,如果休息(xiuxi)日大于0的,取分数(fenshu)高的
      3.如果休息(xiuxi)日小于0,取分数(fenshu)低的如果只能以上数据,结果会是这样id    fenshu   xiuxi    bumen
3     90       2        2求SQL代码或者PHP代码都可以。SQLPHP

解决方案 »

  1.   

       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://bbs.csdn.net/topics/320211382
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。
      

  2.   

    create table staff
    (
    id   int(10),
    fenshu  int(10),
    xiuxi  int(10),
    bumen  int(10),
    )
    insert into staff (id,fenshu,xiuxi,bumen) values(1,80,0,2)
    insert into staff (id,fenshu,xiuxi,bumen) values(2,80,1,2)
    insert into staff (id,fenshu,xiuxi,bumen) values(3,90,2,2)
    insert into staff (id,fenshu,xiuxi,bumen) values(4,60,0,3)/*
    要得到这样的报表:
    id    fenshu   xiuxi    bumen
    3     90       2        2
    4     60       0        3*/
      

  3.   

    mysql> select * 
        -> from staff A
        -> where not exists (select 1 from staff  B where A.bumen=B.bumen and A.fenshu<B.fenshu)
        -> ;
    +------+--------+-------+-------+
    | id   | fenshu | xiuxi | bumen |
    +------+--------+-------+-------+
    |    3 |     90 |     2 |     2 |
    |    4 |     60 |     0 |     3 |
    +------+--------+-------+-------+
    2 rows in set (0.00 sec)