(1)id name
1  aa
2  bb
1  dd
2  ee
我想要的结果是 id唯一的一条记录,但是我又要把name取出来的
1 dd
2 bb
也可以是1 dd
2 eeselect distinct id,name from table where name!=aaselect id,name from table group by id,name这2种方法搜索不出来,都不是我想要的结果(2)用hql语句的话,好像不认识distinct的,分页出来的总记录是没有distinct的那个值

解决方案 »

  1.   


    ---sql有点复杂..呵呵..抛砖引玉吧...好象hql还不行
    SQL> with tab as
      2  (
      3      select 1 id, 'aa' name
      4        from dual
      5      union all
      6      select 2 id, 'bb' name
      7        from dual
      8      union all
      9      select 1 id, 'dd' name
     10        from dual
     11      union all
     12      select 2 id, 'ee' name from dual
     13  )
     14  select t.id, t.name
     15    from tab t,
     16         (select id, name
     17            from (select id,
     18                         name,
     19                         row_number() over(partition by id order by dbms_random.value()) rn
     20                    from tab) tt
     21           where tt.rn = 1) t1
     22   where t1.id = t.id
     23     and t1.name = t.name
     24  /        ID NAME
    ---------- ----
             1 dd
             2 bbSQL> 
    SQL> with tab as
      2  (
      3      select 1 id, 'aa' name
      4        from dual
      5      union all
      6      select 2 id, 'bb' name
      7        from dual
      8      union all
      9      select 1 id, 'dd' name
     10        from dual
     11      union all
     12      select 2 id, 'ee' name from dual
     13  )
     14  select t.id, t.name
     15    from tab t,
     16         (select id, name
     17            from (select id,
     18                         name,
     19                         row_number() over(partition by id order by dbms_random.value()) rn
     20                    from tab) tt
     21           where tt.rn = 1) t1
     22   where t1.id = t.id
     23     and t1.name = t.name
     24  /        ID NAME
    ---------- ----
             1 aa
             2 eeSQL> 
      

  2.   

    SELECT  tab1.ID,tab1.NAME
    from USER tab1 inner join (select ID,max(NAME) as NAME  from USER group by ID)tab2 on tab1.ID=tab2.ID
    where 
    tab1.NAME=tab2.NAME吸铁石童鞋  我来抢分了
      

  3.   

    with temp as(
    select 1 id, 'aa' name from dual
    union all
    select 2 id, 'bb' name from dual
    union all
    select 1 id, 'dd' name from dual
    union all
    select 2 id, 'ee' name from dual
    )
    select t.id,t.name from temp t,
    (
    select id,name from (
    select id,row_number() over(partition by id order by id desc) rn,name from temp
    ) where rn = 1
    ) a where t.id = a.id and t.name != a.name
      

  4.   


    SELECT ID,NAME FROM ( SELECT t.*,RANK() OVER (PARTITION BY ID ORDER BY NAME) rn FROM table1 t) a WHERE a.rn=1;
      

  5.   

    不好意思,楼上的各位,昨天表达的不是很正确id name 
    1  aa 
    2  bb 
    1  dd 
    2  ee 
    这边的id和name是两张表的,就是tab1里面有个主键id,是tab2的外键,而name是tab2的中的一个字段
      

  6.   

    tab1 里面有id,title等等其他字段,然后tab1和tab2的关系是一对多的但是我想要从数据库里面查询出来的数据是以tab1的id为唯一的一条记录,却又要判断tab2的name的值是否为登录的那个人的名字id name
    23  test4
    23  test10好几条记录,而我想要的是23的就只有一条记录,后面的name随便哪一个都可以的
      

  7.   

    你是不是要取得tab1的所有记录,每个id再加上tab2中id相同的任意一个name?SELECT tab1.*,tab2.name FROM tabl,
    (SELECT ID,NAME FROM ( SELECT t.*,RANK() OVER (PARTITION BY ID ORDER BY NAME) rn FROM tab2 t) a WHERE a.rn=1) tab2
    WHERE tab1.id=tab2.id;