select * from userinfo where userinfo.userid = (select distinct news.userid from news where news.parentclassid =1)按你的要求,SQL语句看不出哪个地方可以提高效率或简化了.

解决方案 »

  1.   

    如果说再提高点效率就是用存储过程
    IF EXISTS(SELECT * FROM sysobjects WHERE name='存储过程名')
    DROP PROCEDURE 存储过程名
    GOcreate procedure 存储过程名
    @parentclassid int 
    as
    select   *   from   userinfo   where   userinfo.userid   =   (select   distinct   news.userid   from   news   where   news.parentclassid   =@parentclassid )
    go存储过程名 1理论上来说,用存储过程的效率怎么也比直接的SQL高那么一点点。。
      

  2.   

    不好意思,是我表达的不清楚,我的意思是这样的,我要做一个招聘的系统
    有一个用户表userinfo
    userid(用户id) username(用户名称) 联系信息。还有一个应用表zhaopinid(自动编号)userid(对应userinfo表的用户id) ProvinceId(省份)CityId(城市)在程序中,工作城市是可以多选的,现在要求,当别人查询省份的所有招聘求职的时候,显示userid username ProvinceId
    显示这些内容,用户名不能重复
      

  3.   

    如果是字段太少的话,还可以再添加字段的,因为我的程序才刚刚开始设计,请大家给一个好的设计思路,我用的是sqlserver2005,谢谢
      

  4.   

    简单点的:
    select distinct z.userid,u.username,z.ProvinceId  from zhaopin as z
    left join userinfo as u
    on z.userid=u.userid
    where ProvinceId='要查询的省份' 
    因为涉及2各表,所以运用到集运算(join),效率肯定比从单个表中读取数据要低,但一般应用足够了,除非你的数据表记录极多。
    还有一种不使用集运算的办法:
    select userid,uername,'要查询的省份' as ProvinceId from userinfo
    where userid in (select distinct userid from zhaopin where ProvinceId='要查询的省份')如果一味追求高效的话设计到一个表肯定会高一些,不过会产生大量冗余。
      

  5.   

    建立相关的索引,比如parentclassid
    select uid.userid,userName,parentclassid from userinfo join
    (select userid,parentclassid from news where parentclassid=1 group by userid,parentclassid )uid
    on userinfo.userid=uid.userid
    从news表查出符合记录的userid,然后再跟userinfo关联
    不要去用in
      

  6.   

    你可以看看SQL语句的执行计划。。
    然后再对SQL语句进行优化