比如 数据库 表abc 的数据如下,name     type香蕉     水果
苹果     水果汽车      车
自行车    车狮子      动物
老虎      动物
……
……每种分类只有2个品种,我想实现的查询效果是name1     name2     type
香蕉      苹果      水果
汽车      自行车    车
狮子      老虎      动物
也就是两行的同一个字段的数据要弄到一行去
我用的sql语句是select A.name as name1,B.name as name2,A.type from abc as A left join abc as B on A.type=B.type group by A.type好像这样写不行啊,达不到效果,应该如何做啊?

解决方案 »

  1.   

    select A.name as name1,B.name as name2,A.type from abc as A left join abc as B on A.type=B.type and A.name='苹果' and B.name='香蕉' group by A.type
    上面写错了,我想实现的效果是name1 name2 type
    香蕉 苹果 水果
      

  2.   

    http://mikoo.blog.51cto.com/627637/124516
    参考
      

  3.   

    select A.name1 as name1,B.name1 as name2,A.[type] from [text] as A, [text] as B
    Where  A.[type]=B.[type] and A.[name1]='苹果' and B.name1='香蕉' 
    内连接就OK了
      

  4.   

    SELECT  Type,
            STUFF(( SELECT  ' ' + Name
                    FROM    abc
                    WHERE   Type = T2.Type
                  FOR
                    XML PATH('')
                  ), 1, 1, '') AS Name
    FROM    abc AS T2
    GROUP BY Type
      

  5.   

    (chenghaoorange 还是你的最好用
      

  6.   

    select A.name as name1,B.name as name2,A.type from abc as A,abc as B where A.type=B.type and A.name<>B.name group by type
    这个你试试看,我这边是可以的,截图我发不上。