如一张成绩表中有很多人的成绩,每个人也有很多条记录(每次考试成绩均记录在其中),要查出某人考的最好的一次应如何写??
表a:mz    cj
张三  83
李四  71
张三  82
李四  73
张三  83
李四  75
张三  50
李四  68
张三  92
李四  76张三的最好成绩应为:92

解决方案 »

  1.   

    SELECT MZ,MAX(CJ)CJ FROM TB GROUP BY MZ
      

  2.   

    select max(cj) from
     a
    where mz = '张三'
      

  3.   

    select max(cj) as cj from tb  where mz = '张三' 
      

  4.   


    select mz,max(cj) as cj from a group by mz
      

  5.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-10-29 09:59:59
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go 
    create table [a]([mz] varchar(4),[cj] int)
    insert [a]
    select '张三',83 union all
    select '李四',71 union all
    select '张三',82 union all
    select '李四',73 union all
    select '张三',83 union all
    select '李四',75 union all
    select '张三',50 union all
    select '李四',68 union all
    select '张三',92 union all
    select '李四',76
    --------------开始查询--------------------------
    select max(cj) as cj from a  where mz = '张三' 
    ----------------结果----------------------------
    /* cj
    -----------
    92(1 行受影响)*/
      

  6.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(我是小F,向高手学习)
    -- Date    :2009-10-29 09:59:59
    -- Version:
    --      Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) 
    -- Nov 24 2008 13:01:59 
    -- Copyright (c) 1988-2005 Microsoft Corporation
    -- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
    --
    ----------------------------------------------------------------
    --> 测试数据:[a]
    if object_id('[a]') is not null drop table [a]
    go 
    create table [a]([mz] varchar(4),[cj] int)
    insert [a]
    select '张三',83 union all
    select '李四',71 union all
    select '张三',82 union all
    select '李四',73 union all
    select '张三',83 union all
    select '李四',75 union all
    select '张三',50 union all
    select '李四',68 union all
    select '张三',92 union all
    select '李四',76
    --------------开始查询--------------------------
    select mz,max(cj) as cj from a  where mz = '张三'  group by mz
    ----------------结果----------------------------
    /* mz   cj
    ---- -----------
    张三   92(1 行受影响)*/
      

  7.   

    select max(cj) as cj from tb  where mz = '张三' 
      

  8.   

    --> 测试数据:[tb]
    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([mz] varchar(4),[cj] int)
    insert [tb]
    select '张三',83 union all
    select '李四',71 union all
    select '张三',82 union all
    select '李四',73 union all
    select '张三',83 union all
    select '李四',75 union all
    select '张三',50 union all
    select '李四',68 union all
    select '张三',92 union all
    select '李四',76select * from [tb] t where cj=(select max(cj) from tb where mz=t.mz)/*
    mz   cj
    ---- -----------
    张三   92
    李四   76*/
      

  9.   


    select * from tb t
    where not exists(select * from tb where name=t.name and cj>t.cj)
      

  10.   

    SELECT 
    mz,MAX(cj) as cj 
    FROM 
    TB 
    GROUP BY 
    mz
      

  11.   

    http://topic.csdn.net/u/20080626/00/43d0d10c-28f1-418d-a05b-663880da278a.html?85256
      

  12.   

    SELECT mz,MAX(cj) as cj FROM TB GROUP BY mz
      

  13.   

    SELECT 
        mz,MAX(cj) as cj 
    FROM 
        TB 
    GROUP BY 
        mz
      

  14.   


    create table aa
    (
    mz varchar(8),
    cj int
    )
    goinsert into aa
    select '张三',  83 union all 
    select '李四',  71 union all
    select '张三',  82 union all
    select '李四',  73 union all
    select '张三',  83 union all
    select '李四',  75 union all
    select '张三',  50 union all
    select '李四',  68 union all
    select '张三',  92 union all
    select '李四',  76 select mz,max(cj)from aa group by mz
    --如果是直接找出最大值
    select max(cj) from aa