tbl1
type1  type2    model
id1    id2     model1tbl2
type1   type2   value createdate
id1     id2     value1 2007-1-1
id1     id2     value2 2007-1-2我想得到
model  type1 type2  value
model1  id1   id2   value2
left join
我直接写的
select * from tbl1 
left join tbl2 on tbl1.type1=tbl2.type1 and tbl1.type1=tbl2.type2
where model=model1重复记录了

解决方案 »

  1.   

    select distinct * from tbl1 
    left join tbl2 on tbl1.type1=tbl2.type1 and tbl1.type1=tbl2.type2
    where model=model1
      

  2.   

    为什么value取value2而不是value1?
      

  3.   

    select tbl1.model,tbl1.type1,tbl1.type2,max(tbl2.value) from tbl1 
    left join tbl2 on tbl1.type1=tbl2.type1 and tbl1.type1=tbl2.type2
    group by tbl1.model,tbl1.type1,tbl1.type2
      

  4.   

    tbl1
    type1  type2    model
    id1    id2     model1tbl2
    type1   type2   value createdate
    id1     id2     value1 2007-1-1
    id1     id2     value2 2007-1-2我想得到
    model  type1 type2  value
    model1  id1   id2   value2select *,(select max(value) from tab2 b where b.ty1=a.ty1 and b.ty2=a.ty2) from tab1 a
      

  5.   

    更正:
    select *,
    (select top 1 value  from tab2 b where b.type1 =a.type1 and b.type2  =a.type2  
    order by createdate desc) 
    from tab1 a
      

  6.   

    我要取最新一条。left join有无办法?
      

  7.   


    select a.* from tbl1 a
     left join
       (select * from tbl2 b
         where not exists(select * from tbl2 where type1=b.type1 and  type2=b.type2 and  createdate>b.createdate))c
     on  c.type1 =a.type1 and c.type2  =a.type2
      

  8.   

    weasea(尘一笑)~我用的子查询~~~
    为什么一定要用left join~~~快吗???还是你想知道left join实现
      

  9.   

    本人觉得!一个列的问题没必要去join了!哈哈
      

  10.   

    w75251455(砍破) ( ) ——
    可是当你原来的sql语句比较复杂,而你现在只是要增加一个表某个字段的关联判断;
    这时,你还要修改为更加庞大的子查询吗?
    ——凡事都不能说绝对啊!!!
      

  11.   

    dapei_712(大胚)
    大哥~~~~我2007-2-9 11:46:13 发的言你就别2007-07-04 10:57:56 说啦~~~~~人~每天都在进步~~每天都是每天的样子。。
    -----如果自己会写~~那就用各种方式写一遍~~哪个快用哪个~~~不要活在一个死角里