有下面两个table   table1,table2,如何能得到table3呢?
table1    
产品名称   销售价   
aa         50
bb         100
cc         110table2
产品名称   销售价   
aa         60
dd         120
ee         130我想要的结果是:table3
产品名称   我的销售价   别人的销售价
aa         50             60
bb         100            无该产品
cc         110            无该产品
dd         无该产品            120
ee         无该产品            130希望大家帮忙啊!!!来者有分!!!

解决方案 »

  1.   

    select table1.产品名称,table1.销售价,table2. 销售价 as [别人的销售价] 
    from table1 left join table2 on table1.产品名称=table2.产品名称
      

  2.   

    select x.销售价格 as  我的销售价格,y.销售价格 as 别人的销售价格
    from table1 x,table2 y
    where x.产品名称= y.产品名称
      

  3.   

    select t1.产品名称, t1.销售价 我的销售价, t2.销售价 别人的销售价
    from table1 t1, table2 t2
    where t1.产品名称=t2.产品名称
      

  4.   

    case xxx when  null then xxx else xxx end 
    判断价格为空
      

  5.   

    不要忘记lz的要求还有right join
      

  6.   


    create table  table1
    (
    name varchar(8),
    price int 
    )
    create table  table2
    (
    name varchar(8),
    price int 
    )
    insert into table1 values('aa',50)
    insert into table1 values('bb',100)
    insert into table1 values('cc',110)insert into table2 values('aa',60)
    insert into table2 values('dd',120)
    insert into table2 values('ee',130)select a.name ,a.price ,b.price 
    from table1 a left join  table2 b 
    on a.name=b.name
    union 
    select b.name ,a.price ,b.price 
    from table1 a right join  table2 b 
    on a.name=b.name
      

  7.   


    create table table1  (name varchar(10),price int)  
    insert into table1
    select 'aa',        50 
    union
    select 'bb',        100 
    union
    select 'cc',        110create table table2(name varchar(10),price int)  
    insert into table2
    select 'aa',        60
    union 
    select 'dd',       120 
    union
    select 'ee',        130 select isnull(a.name,b.name) 产品名称,isnull(a.price,0) 我的销售价,isnull(b.price,0) 别人的销售价
    from table1 a
    full join table2 b on a.name=b.name产品名称       我的销售价       别人的销售价
    ---------- ----------- -----------
    aa         50          60
    bb         100         0
    cc         110         0
    dd         0           120
    ee         0           130(5 行受影响)最后你可以用case when 对 销售价格为0的进行处理为“无该产品”。。之类的。。
      

  8.   

    select a.产品名称,a.销售价,isnull(b.销售价,'无该产品 ') from test1 a
    left join test2 b on a.siD=b.sID
    union 
    select a.产品名称,isnull(b.销售价,'无该产品 '),a.销售价 from test2 a
    left join test1 b on a.siD=b.sID
    where b.sID is null
      

  9.   

    select DISTINCT * from
    (select table1.产品名称,table1.销售价,table2.销售价 as 我的销售价 from table1 left join table2 on table1.产品名称=table2.产品名称
    UNION ALL
    select table2.产品名称,table2.销售价 as 我的销售价 from table2 left join table1 on table1.产品名称=table2.产品名称
    ) as table3
      

  10.   

    select isnull(a.name,b.name) 产品名称,isnull(CAST(a.price as varchar),'无该产品') 我的销售价,isnull(cast (b.price as varchar ),'无该产品') 别人的销售价
    from table1 a
    full join table2 b on a.name=b.name14楼很强大
      

  11.   

    我认为创建一个存储过程比较合适.然后在调用存储过程就可以了,代码
    create proc proc_table3
    as
    写上面回复的代码就可以了
    go最后
    exec proc_table3
      

  12.   


    declare @t table([产品名称] varchar(30), [销售价] decimal(10,2))
    insert into @t values('aa'    ,    50 )
    insert into @t values('bb'    ,    100 )
    insert into @t values('cc'    ,    110 )declare @t1 table([产品名称] varchar(30), [销售价] decimal(10,2))
    insert into @t1 values('aa'     ,   60    )
    insert into @t1 values('dd'     ,   120    )
    insert into @t1 values('ee'     ,   130   )select isnull(a.[产品名称] ,b.[产品名称]) as [产品名称],
           isnull(Convert(varchar(50),a.[销售价]) ,'无该产品') as [我的销售价],
           isnull(Convert(varchar(50),b.[销售价]) ,'无该产品') as [别人的销售价] from @t a 
    FULL outer  JOIN @t1 b on a.[产品名称] = b.[产品名称]order by [产品名称]
      

  13.   


    正解,还有没有人提select into?她要table3哦
      

  14.   

    select table1.产品名称,table1.销售价,table2. 销售价 as [别人的销售价] 
    from table1 left join table2 on table1.产品名称=table2.产品名称
      

  15.   

    create table  table1 

    name varchar(8), 
    price varchar(30) 

    create table  table2 

    name varchar(8), 
    price varchar(30)

    insert into table1 values('aa',50) 
    insert into table1 values('bb',100) 
    insert into table1 values('cc',110) insert into table2 values('aa',60) 
    insert into table2 values('dd',120) 
    insert into table2 values('ee',130) 
    select a.name ,a.price ,b.price 
    from table1 a left join  table2 b 
    on a.name=b.name 
    union 
    select b.name ,a.price ,b.price 
    from table1 a right join  table2 b 
    on a.name=b.name
     
     select t.name , isnull(t1.price,'无价格') as 我的价格 ,isnull(t2.price,'无价格') as 其他价格 from 
    (select table1.name from table1 union select table2.name from table2) as t left join table1 as t1 on t.name=t1.name
    left join table2 as t2 on t.name=t2.name
    drop table table2
    优化了下  速度应该不错
      

  16.   

    其实sql的问题,可以发到sql区的。
      

  17.   

    谢谢各位朋友啊。我上面的table  不是数据库中的表   是datatable
      

  18.   


    这个怎么办呢? 可是你的datatable是怎么来的呢?别人传递过来的?
      

  19.   

    datatable可以考虑用linq来实现     把上面达人的sql转成linq语法 就差不多了   可以试下!~~~
      

  20.   

    create table table1 (proName varchar(50),price decimal(10,2));
    create table table2 (proName varchar(50),price decimal(10,2));insert into table1(proName,price) values('aa',50.00);
    insert into table1(proName,price) values('bb',100.00);
    insert into table1(proName,price) values('cc',110.00);insert into table2(proName,price) values('aa',60.00);
    insert into table2(proName,price) values('dd',120.00);
    insert into table2(proName,price) values('ee',130.00);
    select a.proName,a.price,b.price as '别人的销售价' from table1 a left outer join table2 b 
    on a.proName=b.proName
      union 
    select b.proName,a.price,b.price as '别人的销售价' from table2 b left outer join table1 a 
    on b.proName=a.proName
    这个应该是可以的,我运行出来结果了
    aa 50.00 60.00
    bb 100.00 NULL
    cc 110.00 NULL
    dd NULL 120.00
    ee NULL 130.00
    当然了,结果自己还需要调整,
      

  21.   

    也是可以用的,
    做个store procedure
    datatable call 这个store procedure
    返回一个DT,
    效果一样的
      

  22.   


    DataTable dtPrice = new DataTable("dtPrice");
                dtPrice.Columns.Add("Name", typeof(string));
                dtPrice.Columns.Add("Price1", typeof(double));
                dtPrice.Rows.Add("aa", "43.0");
                dtPrice.Rows.Add("bb", "63.0");
                dtPrice.Rows.Add("cc", "73.0");
                dtPrice.Rows.Add("dd", "33.0");            DataTable dtPrice2 = new DataTable("dtPrice2");
                dtPrice2.Columns.Add("Name", typeof(string));
                dtPrice2.Columns.Add("Price2", typeof(double));            dtPrice2.Rows.Add("aa", "93.0");
                dtPrice2.Rows.Add("cc", "83.0");
                dtPrice2.Rows.Add("ee", "13.0");            //使用下面两种之一;            // 列 Price1,Price2 的类型为String类型 默认值:无此产品
                //dtPrice.Columns["Price1"].DefaultValue = "无该产品";
                //dtPrice2.Columns["Price2"].DefaultValue = "无该产品";            // 列Price1,Price2 的类型为值类型 添加新一列 并创建表达式;
                dtPrice.Columns.Add("PriceStr1", typeof(string), "ISNULL(Price1,'无该产品')");
                dtPrice2.Columns.Add("PriceStr2", typeof(string), "IIF(Price2 is null,'无该产品',Price2)");            //设置主键 
                dtPrice.PrimaryKey = new DataColumn[] { dtPrice.Columns["Name"] };
                //合并到第一个表
                dtPrice.Merge(dtPrice2, false, MissingSchemaAction.Add);