刚发的帖子结贴了...
然后自己看了下回帖
select t.* from a t where y = (select max(y) from a where x = t.x)select t.* from a t where not exists (select 1 from a where x = t.x and y > t.y)
知道可以实现
但不是很清楚原理...
求高手解释下....
先谢啦...

解决方案 »

  1.   

    分别比较两表相同的x,取最大的Y值.select t.* from a t where y = (select max(y) from a where x = t.x)分别比较两表相同的X,只到没有比Y最大的值还大的值了.select t.* from a t where not exists (select 1 from a where x = t.x and y > t.y)如果看不懂解释的话,去帮助中查询not exists
      

  2.   

    使用 EXISTS 和 NOT EXISTS 查找交集与差集
    使用 EXISTS 和 NOT EXISTS 引入的子查询可用于两种集合原理的操作:交集与差集。两个集合的交集包含同时属于两个原集合的所有元素。差集包含只属于两个集合中的第一个集合的元素。city 列中 authors 和 publishers 的交集是作者和出版商共同居住的城市的集合。USE pubs
    SELECT DISTINCT city
    FROM authors
    WHERE EXISTS
       (SELECT *
       FROM publishers
       WHERE authors.city = publishers.city)下面是结果集:city
    --------
    Berkeley(1 row(s) affected)当然,该查询可以写成一个简单的联接。USE pubs
    SELECT DISTINCT authors.city
    FROM authors INNER JOIN publishers
    ON authors.city = publishers.citycity 列中 authors 和 publishers 的差集是作者所居住的、但没有出版商居住的所有城市的集合,也就是除 Berkeley 以外的所有城市。USE pubs
    SELECT DISTINCT city
    FROM authors
    WHERE NOT EXISTS
       (SELECT *
       FROM publishers
       WHERE authors.city = publishers.city)该查询也可以写成:USE pubs
    SELECT DISTINCT city
    FROM authors
    WHERE city NOT IN
       (SELECT city 
       FROM publishers)©1988-2000 Microsoft Corporation。保留所有权利。
      

  3.   

    这样的别名会不会好理解点?select me.* from a me where y = (select max(y) from a where x = me.x)
    select me.* from a me where not exists (select 1 from a where x = me.x and y > me.y)
      

  4.   

    谢谢各位了....
    sql比较菜,各位见谅....
    特此感谢'爱新觉罗.毓华'