如题!
例如:
数据库内容如下:
字段1  字段2
 1      2
 1      3
 1      4
我要查询到的结果是:字段1只查到1,字段2查到2,3,4
我是分两个查询做的
不知道能不能用一个语句就可以查询到。

解决方案 »

  1.   

    select col1,col2 from tb where col1=1 and col2 in(2,3,4)   --这样?
      

  2.   

    可以的,
    select *
    from 第一个查询 k cross join 第二个查询 l
      

  3.   

    select col1,col2 from tb where col1*col2 in(2,3,4)  
      

  4.   


    declare @str varchar(1000)
    set @str=''
    SELECT @str=@str+rtrim(col2)+',' FROM [test] where col1=1 
    select col1,@str from test where col1=1 group by col1col1    无列名
    1 2,3,4,
      

  5.   

    一楼和四楼 写得sql语句  达不到 楼主想要的结果,你们得出的结果和原表一样的。我觉得六楼写得还有点对,但是看的不是太明白  有雪 能不能给小弟讲一下。 赐教  谢谢
      

  6.   

    六楼的朋友 我觉得你写的是针对楼主举得那个例子,但是我觉得应该按下面的写:declare @str varchar(1000)
    set @str=''
    SELECT top 1  col1, @str col2  FROM [test]
     where col1=1 and @str=(SELECT @str=@str+rtrim(col2)+',' FROM [test] where col1=1)结果:
      col1   col2
       1    2,3,4,
    但是我有点怀疑 SELECT @str=@str+rtrim(col2)+',' FROM [test] where col1=1 这句话能不能出现结果 2,3,4,。
      

  7.   

    好像解决不了,由于我用的是VB2008,到好VB.NET去提问试试看,能不能解决问题
    多谢各位了!