create table form1 
(
  id int , name char(20), Qty  int
)insert form1 select 1,'HU',20 union all                  select 2,'HU',40 union all
                 
                  select 3,'HY',40 union all                  select 1,'JACK',30
---------------------------------------------------------
原要求是 取出:不同id,姓名且Qty每条中的最大值
即:2 HU  40
       3 HY 40
       1  JACK  30
解答语句有:法一
select id,name,qtyfrom form1 twhere not exists(select 1 from form1 where t.id<id and t.name=name)法二
select * 
from tb t
where qty=(select max(qty)
           from tb
           where name=t.name)
------------------------------------------------------------
但对以上二条句子不理解(原因由下语句限制思维select  a.*, b. * from form1  a, form1 b where a.name=b.name and a.id<b.id
select *from form1 a inner join form1 b on a.name=b.name and a.id<b.id
执行结果是  1 HU 20 | 2 HU 40
-------》上一行是显示两条记录
1 为什么select id,name,qty from form1 t
where not exists(select 1 from form1 where t.id<id and t.name=name)

select * from tb t where qty=(select max(qty)
           from tb
           where name=t.name)
没有冗余列,而这里为什么一行只显示一条记录,分析一下语法和原因。
       2 HU 40   |???
       3 HY  40   |? ? ?
       1 JACK  30 |? ? ?请各位大侠针对问题再举例讲解 或 链接 自连接知识

解决方案 »

  1.   

    解答语句有:法一
    select id,name,qtyfrom form1 twhere not exists
    (select 1 from form1 where t.id<id and t.name=name)
    最后一句的意思是,在子表中不存在id大于原表的资料,及你取得的ID是最大的。方法二也是一样的理解,更直接些,取得最大值而已。
      

  2.   

    法一
    select id,name,qtyfrom form1 twhere not exists(select 1 from form1 where t.id<id and t.name=name)自己与自己比较当姓名相同去id大的
      

  3.   

    法二
    select * 
    from tb t
    where qty=(select max(qty)
               from tb
               where name=t.name)当姓名相同取qty最大的
      

  4.   


    where t.name=name --其实相当于group by nameselect max(qty)
               from tb
               where name=t.name
    使用聚合函数max ,默认其实也是group by name,t.id<id
    因为id是不重复的,排序的,这里相当于group by id
      

  5.   

    select 1 from form1 where t.id<id and t.name=name
    但这条求最大值的 子查询只是返回一个值
      

  6.   

    这有什么难理解?select id,name,qty from form1 t where not exists(select 1 from form1 where t.id<id and t.name=name)
    select 这些记录 from form1 as 我 where 不存在 (select 任何记录 from form1 where name = 我的name 同时 id > 我的id)select * from # t where qty=(select max(qty) from # where name=t.name)
    select 这些记录 from form1 as 我 where qty=(select 最大的qty from # where name = 我的name)这两条语句并不是一回事,将1/2的qty对调再执行就会清楚。
      

  7.   

    select id,name,max(qty) from form1
    group by id,name
      

  8.   

    如果按照要求的结果,即取出:不同id,姓名且Qty每条中的最大值
    方法一是不能达到要求的,可将插入的第一行数据的Qty值由20修改为大于第四行30的任一数值,最后的结果依然会输出:1  JACK  30。
      

  9.   

    不明白类似 a.name=b.name and a.id<b.id 查询的语句
    如。。inner join on a.name=b.name 出现 一行相交的两条记录
    而下面语句不出现
    select id,name,qty from form1 t
    where not exists(select 1 from form1 where t.id<id and t.name=name)
      

  10.   

    select  a.*, b. * from form1  a, form1 b where a.name=b.name and a.id<b.id
    select *from form1 a inner join form1 b on a.name=b.name and a.id<b.id