用SQL,如何从table1和table2,得出table3?table1
种类    颜色
苹果    红色
梨子    黄色
西瓜    绿色
榴莲    黄色
提子    紫色
荔枝    红色
table2
种类    单价
苹果    10
梨子    5
西瓜    2
榴莲    20
杏子    7
杨桃    4table3
种类    颜色    单价
苹果    红色    10
梨子    黄色    5
西瓜    绿色    2
榴莲    黄色    20

解决方案 »

  1.   


    with table1(typ,clr)AS(
        select N'苹果',N'红色' union all
        select N'梨子',N'黄色' union all
        select N'西瓜',N'绿色' union all
        select N'榴莲',N'黄色' union all
        select N'提子',N'紫色' union all
        select N'荔枝',N'红色'
    ),table2(typ,price)AS(
        select N'苹果',10 union all
        select N'梨子',5 union all
        select N'西瓜',2 union all
        select N'榴莲',20 union all
        select N'杏子',7 union all
        select N'杨桃',4
    )
    select * from table1 as t1 inner join table2 as t2 on t1.typ=t2.typ
       typ clr typ price
    1 苹果 红色 苹果 10
    2 梨子 黄色 梨子 5
    3 西瓜 绿色 西瓜 2
    4 榴莲 黄色 榴莲 20
      

  2.   

    连接查询
    select * from table1 join table2 on (table1.种类=table2.种类)
      

  3.   

    做个 join连接就可以了
      

  4.   

    --测试数据
    if not object_id(N'Tempdb..#T1') is null
    drop table #T1
    Go
    Create table #T1([种类] nvarchar(22),[颜色] nvarchar(22))
    Insert #T1
    select N'苹果',N'红色' union all
    select N'梨子',N'黄色' union all
    select N'西瓜',N'绿色' union all
    select N'榴莲',N'黄色' union all
    select N'提子',N'紫色' union all
    select N'荔枝',N'红色'
    GO
    if not object_id(N'Tempdb..#T2') is null
    drop table #T2
    Go
    Create table #T2([种类] nvarchar(22),[单价] int)
    Insert #T2
    select N'苹果',10 union all
    select N'梨子',5 union all
    select N'西瓜',2 union all
    select N'榴莲',20 union all
    select N'杏子',7 union all
    select N'杨桃',4
    Go
    --测试数据结束
    Select * from #T1 JOIN #T2 ON #T2.种类 = #T1.种类
      

  5.   

    --测试数据
    if not object_id(N'Tempdb..#T1') is null
    drop table #T1
    Go
    Create table #T1([种类] nvarchar(22),[颜色] nvarchar(22))
    Insert #T1
    select N'苹果',N'红色' union all
    select N'梨子',N'黄色' union all
    select N'西瓜',N'绿色' union all
    select N'榴莲',N'黄色' union all
    select N'提子',N'紫色' union all
    select N'荔枝',N'红色'
    GO
    if not object_id(N'Tempdb..#T2') is null
    drop table #T2
    Go
    Create table #T2([种类] nvarchar(22),[单价] int)
    Insert #T2
    select N'苹果',10 union all
    select N'梨子',5 union all
    select N'西瓜',2 union all
    select N'榴莲',20 union all
    select N'杏子',7 union all
    select N'杨桃',4
    Go
    --测试数据结束
    Select #T1.*,#T2.[单价] from #T1 JOIN #T2 ON #T2.种类 = #T1.种类
      

  6.   

    我只是用'ch21st'的改下
      

  7.   

    做个join的连接啊