table1的id全部会在table2种出现么?select id,max(num),result
from table1
where id in (select id from table2)
group by id,result

解决方案 »

  1.   

    select a.* form table1 a where num in(select max(num) from table1 where id=a.id)
      

  2.   

    create table table1(id int,num varchar(10),result varchar(10))
    Insert into table1 
    select '1','11','aa'
    union all select '1','22','gg'
    union all select '1','14','ii'
    union all select '2','13','bb'
    union all select '3','14','cc'
    union all select '3','05','kk'
    union all select '4','11','dd'select a.* from table1 a where num in(select max(num) from table1 where id=a.id) order by id--結果
    id    num   result
    ---------------------
    1 22 gg
    2 13 bb
    3 14 cc
    4 11 dd
      

  3.   

    select b.* from table2 a left join
    (
    select * from table1 a join 
    (select id,max(num)as num from table1 group by id)b 
    on a.id=b.id and a.num=b.num
    )b on a.id=b.id where b.id is not null order by a.id
      

  4.   

    也可以這樣寫,答案一樣create table table1(id int,num varchar(10),result varchar(10))
    Insert into table1 
    select '1','11','aa'
    union all select '1','22','gg'
    union all select '1','14','ii'
    union all select '2','13','bb'
    union all select '3','14','cc'
    union all select '3','05','kk'
    union all select '4','11','dd'select a.* from table1 a,(select id,num=max(num) from table1 group by id)b
    where a.id=b.id and a.num=b.num
    order by a.id
    --結果
    id    num   result
    ---------------------
    1 22 gg
    2 13 bb
    3 14 cc
    4 11 dd
      

  5.   

    create table table1(id int,num varchar(10),result varchar(10))
    Insert into table1 
    select '1','11','aa'
    union all select '1','22','gg'
    union all select '1','14','ii'
    union all select '2','13','bb'
    union all select '3','14','cc'
    union all select '3','05','kk'
    union all select '4','11','dd'
    create table table2(id int)
    insert into table2 select 1
    insert into table2 select 3
    insert into table2 select 4select b.* from table2 a left join
    (
    select a.* from table1 a join 
    (select id,max(num)as num from table1 group by id)b 
    on a.id=b.id and a.num=b.num
    )b on a.id=b.id where b.id is not null order by a.iddrop table table1
    drop table table2
      

  6.   

    更正,應該這樣寫,剛才忘了table2create table table1(id int,num varchar(10),result varchar(10))
    Insert into table1 
    select '1','11','aa'
    union all select '1','22','gg'
    union all select '1','14','ii'
    union all select '2','13','bb'
    union all select '3','14','cc'
    union all select '3','05','kk'
    union all select '4','11','dd'create table table2(id int)
    Insert into table2 
    select '1'
    union all select '3'
    union all select '4'select b.* from table2 a,(select a.* from table1 a where num in(select max(num) from table1 where id=a.id))b
    where a.id=b.id--結果
    id    num   result
    ---------------------
    1 22 gg
    3 14 cc
    4 11 dd
      

  7.   

    create table table1(id int,num varchar(10),result varchar(10))
    Insert into table1 
    select '1','11','aa'
    union all select '1','22','gg'
    union all select '1','14','ii'
    union all select '2','13','bb'
    union all select '3','14','cc'
    union all select '3','05','kk'
    union all select '4','11','dd'create table table2(id int)insert into table2
    select 1 union all
    select 3 union all
    select 4 select * from table1 a  join table2 b on a.id=b.id
    where not exists(select * from table1 where num>a.num and id=a.id)
    order by a.id
    -----------------------------------------------------------------
    1 22 gg 1
    3 14 cc 3
    4 11 dd 4(所影响的行数为 3 行)
      

  8.   

    改一下
    select a.* from table1 a  join table2 b on a.id=b.id
    where not exists(select * from table1 where num>a.num and id=a.id)
    order by a.id
    -----------------------------------------------------------------
    1 22 gg
    3 14 cc
    4 11 dd
      

  9.   

    select  b.*,a.result
    from table1 a,
    (
    select id,max(num) as num
    from table1
    group by id) b
    where a.id=b.id and a.num=b.num
    order by a.id
      

  10.   

    楼主的table2好像不需要,呵呵
      

  11.   

    也可以这样写:select a.* from table1 as a where a.id in (select id from table2) and not exists (select * from table1 where id=a.id and num>a.num) order by a.id