我想问个问题 就是 出现这样的 格式  
  code name ax ay link
  
  1001 dd 11 33 http://google.cn
  1001 dd 22 33 kkkkk
  1002 ss ff ss kjjljlj
  1003 ww f2f dfs jkjljlkj  
  1003 ww jkjl jljljk jljljljl  我想写 一 SQL 语句,,查询 只有一条的数据 比如1001有 两条 ,,但我只想取其 1条。。  这样的 SQL 语句如何写呢???
  请大家 能多帮帮忙,,在线等。。谢谢大家了。。

解决方案 »

  1.   

    select distinct(code) from table
      

  2.   

    用distinct 去掉重复的
    select distinct code 
      

  3.   

    select distinct(code) from table
      

  4.   

    select distinct(code) from table
    top 1
      

  5.   

    select distinct(code) from table
      

  6.   

    1001两条数据不是完全一样的,你1001不是主键吧,说明1001可以有重复值,你只想取1001的其中一条,你得多加几个条件才行!有两条,你想取一条,想取哪一条呢,where条件决定!
      

  7.   

    select * from 表 a where ax = (select ax from 表 where a.code=表.code)这样写
      

  8.   

    select * from 表 a where ax = (select top 1 ax from 表 where a.code=表.code)这样写
      

  9.   

    select distinct(code) from tabledistinct是去掉重复行的
      

  10.   

    select * from table where not exists (select 1 from table a where a.code=table.code and a.link>table.link)
      

  11.   

    create table #tblOneTmp(codeNew nvarchar(50),linkNew nvarchar(1000))insert into #tblOneTmp(codeNew,linkNew)
    select code,(select top 1 link from tbl1 where tbl1.code=tblTmp.code) as linkNew
    from (select code from tbl1 group by code )as tblTmpselect * from #tblOneTmpdrop table #tblOneTmp
      

  12.   

    select code,name,min(ax) ax, min(ay) ay,min(link) link from tabname group by code,name
      

  13.   

    select * from 表 a where ax = (select top 1 ax from 表 where a.code=表.code)这样写
      

  14.   

    楼主是要所有字段又是group by 分组的问题楼主你要考虑一个问题 
    1001 dd 11 33 http://google.cn
      1001 dd 22 33 kkkkk
      1002 ss ff ss kjjljlj1001 你只取一条,但你取哪一条??? 后面的字段可不一样啊???!! 你没有告诉我们  这个语句无法写!
      

  15.   

    select code,name,ax,ay,link from table group by code,name,ax,ay,link 
      

  16.   

    效率不高
    select top 1 * from table where code in (select code from table group by code)每天回帖
      

  17.   

    select * from table where count(name)=1
      

  18.   

    查询 只有一条的数据  select TOP 1 * from table1如果多条都相同 只取一条 select distinct(code) from table2
      

  19.   

    select distinct(code) from table
    select top 1 * from table
      

  20.   

    select * from 表 a where code= (select max(code) from 表 where a.code=表.code)
      

  21.   

    declare @a table (
        code varchar(50) ,
        [name] varchar(50) ,
        ax varchar(50) ,
        ay varchar(50) ,
        link varchar(50)
    )declare @b table (
        rowno int ,
        code varchar(50) ,
        [name] varchar(50) ,
        ax varchar(50) ,
        ay varchar(50) ,
        link varchar(50)
    )insert into @a
    select '10001', 'dd', '11', '33', 'http://google.cn' union all
    select '10001', 'dd', '22', '33', 'kkkkk' union all
    select '10002', 'ss', 'ff', 'ss', 'kjjljlj' union all
    select '10003', 'ww', 'f2f', 'dfs', 'jkjljlkj' union all
    select '10003', 'ww', 'jkjl', 'jljljk', 'jljljljl'select * from @ainsert into @b
    select row_number() over(order by [name], ax, ay, link) as rowno, * from @aselect * from @bselect * from @b where rowno in (
        select max(rowno) from @b group by code
    )
      

  22.   

    用distinct关键字
    接分
    O(∩_∩)O~