select distinct * from
(........) t

解决方案 »

  1.   

    select distinct * from 
    (a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education     from   UserPhoto   b   ,UserBase   a   left   join     ZhiJianPaiMing   c 
    on   a.id=c.userid   and   getdate()> c.starttime   and   getdate() <c.endtime   where   a.ID=B.UserID   and   a.Sex   =1     Order   By   case   when   c.id   is   null   then   1   else   0   end,a.addTime ) t
      

  2.   

    研究过了,先放到一个临时表里然后再用select distinct筛选啊!!
      

  3.   

    可用用Group by来代替distinct,再用min函数来进行排序的设定select   a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education,min(case   when   c.id   is   null   then   1   else   0   end) as id1,min(a.addTime) as id2     from   UserPhoto   b   ,UserBase   a   left   join     ZhiJianPaiMing   c 
    on   a.id=c.userid   and   getdate()> c.starttime   and   getdate() <c.endtime   where   a.ID=B.UserID   and   a.Sex   =1  Group by  a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education   Order   By   id1,id2随便修改了一下,由于太长不知是否正确,呵呵
    这样说吧,研理一样,有一个表a中有两个字段id,mob,id是自动递增的,mob有重复,如果要找出按id排序的前20个mob不重复的记录,可以这样写 
    select top 20 mob,min(id) as ids from a group by mob order by ids
      

  4.   

    select  DISTINCT   a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education     from   UserPhoto   b   ,UserBase   a   left   join     ZhiJianPaiMing   c 
    on   a.id=c.userid   and   getdate()> c.starttime   and   getdate() <c.endtime   where   a.ID=B.UserID   and   a.Sex   =1     Order   By   case   when   c.id   is   null   then   1   else   0   end,a.addTime 
    有重复的说明查询时为完全过滤掉 ,而用distinct的用就是这个,要想不用他,你把查询在详细一下
    具体不想看了
      

  5.   


    case   when   c.id   is   null   then   1   else   0   end as a --放在select 前面 
      

  6.   

    这个简单啊,偶给你解决了
    SELECT  [a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education] FROM (SELECT DISTINCT * FROM [UserPhoto b ,UserBase a left join  ZhiJianPaiMing c 
    on a.id=c.userid and getdate()>c.starttime and getdate() <c.endtime]) as new WHERE a.ID=B.UserID and a.Sex =1 ORDER BY case when c.id is null then 1 else 0 end,a.addTime 
    你的代码内容太多,我怕改的时候有写错,我把方法告诉你:
    SELECT  [要查询的列] FROM (SELECT DISTINCT * FROM [要查询表的表名]) as new WHERE 判断条件 ORDER BY 排序的列的列名
    new是新的表名,可以随意取。
      

  7.   

    把星号改成要显示的列名,ORDER BY 后面的排序的列的列名 要从要显示的列里取
      

  8.   

    如果你一定要按照某方向排列,可以在数据库新建一列,如我想要按照自动排序的id方向排列,我可以建立一个和id同方向的列sid,
    如下:
    表名:use
    id   name   sex   sid
    1     你      男     1
    2     你      男     1
    3     我      男     2
    4     我      男     2
    5     我      男     2
    6     我      男     2
    7     她      女     3
    8     她      女     3SELECT [name] FROM (SELECT DISTINCT name,sid,sex FROM [use]) as new WHERE sex="男" ORDER BY sid
    我只是提供个想法,具体实现看你自己哇
      

  9.   


        * 首页|
        * 空间|
        * 新闻|
        * 论坛|
        * 博客|
        * 下载|
        * 读书|
        * 网摘|
        * 书店|
        * 程序员|
        * 项目交易|
        * 乐知教育|
        * 网址    * 欢迎您:游客|退出|登录注册|帮助CSDN-CSDN社区-MS-SQL Server-疑难问题    * 管理菜单
              o 置顶
              o 推荐
              o 取消推荐
              o 锁定
              o 解锁
              o 移动
              o 编辑
              o 删除
              o 帖子加分
              o 生成帖子
        * 结  帖
        * 发  帖
        * 回  复收藏 如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中。[问题点数:20分,无满意答案结帖,结帖人:tglflzqlmy]    *
        * tglflzqlmy
        * (唐古拉风)
        *
        * 等 级:
        * 结帖率:94.79%
        *
    楼主发表于:2008-01-19 18:07:29
      select DISTINCT a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education  from UserPhoto b ,UserBase a left join  ZhiJianPaiMing c
    on a.id=c.userid and getdate()>c.starttime and getdate() <c.endtime where a.ID=B.UserID and a.Sex =1  Order By case when c.id is null then 1 else 0 end,a.addTime这个SQL语名,如果加上了DISTINCT就报错,          (如果指定了 SELECT DISTINCT,那么 ORDER BY 子句中的项就必须出现在选择列表中。)
    如果去掉DISTINCT就多了许多得复的结果唉,老外开发的程试就难理解,晕,哪个高手帮下           * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP回复次数:12    * dawugui用户头像
        * dawugui
        * (爱新觉罗.毓华(十八年风雨,守)
        *
        * 等 级:
        *      5
          更多勋章
    #1楼 得分:0回复于:2008-01-19 18:16:14
    select distinct * from
    (........) t    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP精华推荐:把两个表的数据进行整合 得出一组数据(求助)    * dawugui用户头像
        * dawugui
        * (爱新觉罗.毓华(十八年风雨,守)
        *
        * 等 级:
        *      5
          更多勋章
    #2楼 得分:0回复于:2008-01-19 18:17:02SQL code    select distinct * from 
        (a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education     from   UserPhoto   b   ,UserBase   a   left   join     ZhiJianPaiMing   c 
        on   a.id=c.userid   and   getdate()> c.starttime   and   getdate()     * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP精华推荐:征婚贴能推荐不?    * tglflzqlmy用户头像
        * tglflzqlmy
        * (唐古拉风)
        *
        * 等 级:
        *
    #3楼 得分:0回复于:2008-01-19 18:52:20
    报,号错误了,二楼的朋友
    我要冷静,再冷静!!!!!!    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP精华推荐:取时间总数的问题,时间可能重复不连续    * fcuandy用户头像
        * fcuandy
        * (顾湘云)
        *
        * 等 级:
        *      3
    #4楼 得分:0回复于:2008-01-19 19:47:44
    学习    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP精华推荐:权限数据库设计问题    * tglflzqlmy用户头像
        * tglflzqlmy
        * (唐古拉风)
        *
        * 等 级:
        *
    #5楼 得分:0回复于:2008-01-21 10:18:44
    还有人帮下忙么?    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP精华推荐:【SQL SERVER2005页面存储3之--聚集索引的存储】    * yangjiexi用户头像
        * yangjiexi
        * (小草)
        *
        * 等 级:
        *
    #6楼 得分:0回复于:2008-01-21 10:53:40
    研究过了,先放到一个临时表里然后再用select distinct筛选啊!!    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP    * Flashcom用户头像
        * Flashcom
        * (虫虫)
        *
        * 等 级:
        *
    #7楼 得分:0回复于:2008-01-23 00:36:05
    可用用Group by来代替distinct,再用min函数来进行排序的设定select  a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education,min(case  when  c.id  is  null  then  1  else  0  end) as id1,min(a.addTime) as id2    from  UserPhoto  b  ,UserBase  a  left  join    ZhiJianPaiMing  c
    on  a.id=c.userid  and  getdate()> c.starttime  and  getdate() <c.endtime  where  a.ID=B.UserID  and  a.Sex  =1  Group by  a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education  Order  By  id1,id2随便修改了一下,由于太长不知是否正确,呵呵
    这样说吧,研理一样,有一个表a中有两个字段id,mob,id是自动递增的,mob有重复,如果要找出按id排序的前20个mob不重复的记录,可以这样写
    select top 20 mob,min(id) as ids from a group by mob order by ids    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP    * JL99000用户头像
        * JL99000
        * (金靓)
        *
        * 等 级:
        *
    #8楼 得分:0回复于:2008-01-23 09:03:08
    select  DISTINCT  a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education    from  UserPhoto  b  ,UserBase  a  left  join    ZhiJianPaiMing  c
    on  a.id=c.userid  and  getdate()> c.starttime  and  getdate() <c.endtime  where  a.ID=B.UserID  and  a.Sex  =1    Order  By  case  when  c.id  is  null  then  1  else  0  end,a.addTime
    有重复的说明查询时为完全过滤掉 ,而用distinct的用就是这个,要想不用他,你把查询在详细一下
    具体不想看了    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP    * roy_88用户头像
        * roy_88
        * (中国风)
        *
        * 等 级:
        *      2      4
    #9楼 得分:0回复于:2008-01-23 09:07:32case  when  c.id  is  null  then  1  else  0  end as a --放在select 前面    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP    * lanfeng214231075用户头像
        * lanfeng214231075
        * (lanfeng214231075)
        *
        * 等 级:
        *
    #10楼 得分:0回复于:2009-04-20 16:58:22
    这个简单啊,偶给你解决了
    SELECT  [a.ID,a.sex,a.NickName,a.Work_location,a.Birth,a.Height,a.Home_location,a.Income,a.Marriage,a.Industry,a.Education] FROM (SELECT DISTINCT * FROM [UserPhoto b ,UserBase a left join  ZhiJianPaiMing c
    on a.id=c.userid and getdate()>c.starttime and getdate() <c.endtime]) as new WHERE a.ID=B.UserID and a.Sex =1 ORDER BY case when c.id is null then 1 else 0 end,a.addTime
    你的代码内容太多,我怕改的时候有写错,我把方法告诉你:
    SELECT  [要查询的列] FROM (SELECT DISTINCT * FROM [要查询表的表名]) as new WHERE 判断条件 ORDER BY 排序的列的列名
    new是新的表名,可以随意取。    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP    * lanfeng214231075用户头像
        * lanfeng214231075
        * (lanfeng214231075)
        *
        * 等 级:
        *
    #11楼 得分:0回复于:2009-04-20 17:16:13
    把星号改成要显示的列名,ORDER BY 后面的排序的列的列名 要从要显示的列里取    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP    * lanfeng214231075用户头像
        * lanfeng214231075
        * (lanfeng214231075)
        *
        * 等 级:
        *
    #12楼 得分:0回复于:2009-04-20 17:36:35
    如果你一定要按照某方向排列,可以在数据库新建一列,如我想要按照自动排序的id方向排列,我可以建立一个和 id同方向的列sid,
    如下:
    表名:use
    id  name  sex  sid
    1    你      男    1
    2    你      男    1
    3    我      男    2
    4    我      男    2
    5    我      男    2
    6    我      男    2
    7    她      女    3
    8    她      女    3SELECT [name] FROM (SELECT DISTINCT name,sid,sex FROM [use]) as new WHERE sex="男" ORDER BY sid
    我只是提供个想法,具体实现看你自己哇    * 对我有用[0]
        * 丢个板砖[0]
        * 引用
        * 举报
        * 管理
        * TOP    * 管理菜单
              o 置顶
              o 推荐
              o 取消推荐
              o 锁定
              o 解锁
              o 移动
              o 编辑
              o 删除
              o 帖子加分
              o 生成帖子
        * 结  帖
        * 发  帖
        * 回  复相关问题
        关于SELECT DISTINCT 求助Web 开发/ ASP - CSDN社区community.csdn.net
        在线等候...急.......order by newid()用法....... MS-SQL Server ...
        谁有用ado访问access数据库出现错误时返回的错误代码的说明? Delphi ...
        [SQL Server]如果指定了SELECT DISTINCT,那么ORDER BY 子句中的项就 ...
        求一个sql语句,想想很简单,可就是实现不了MS-SQL Server / 疑难问题 ...
        急!!!distinct的使用,在线等!! MS-SQL Server / 疑难问题- CSDN ...     公司简介|招贤纳士|广告服务|银行汇款帐号|联系方式|版权声明|法律顾问|问题报告
        北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
        世纪乐知(北京)网络技术有限公司 提供技术支持
        Email:[email protected]
        Copyright © 1999-2010, CSDN.NET, All Rights Reserved
        GongshangLogo