你从城市人员表里面选出男性,60岁以上的人。我:select * from country_population where sex=1 and age>60还有写法吗?
汗……我只会这种
憋曲了半天
select * from country_population where sex=1 and id in
(select id from country_population where age>60)我要的是比第一种效率高,比第二种简单的
更汗……
我:“是不是一张表??”
“对,就一张表”
是不是主键只有id?
对,只有id我考……真的不会了,还有什么写法啊?

解决方案 »

  1.   

    将 select * 换成 select 字段1,字段2
      

  2.   

    select * from country_population where age>60 and sex=1 
    这样写效率高,因为sql语句中where 条件是从右到左进行的,当然要把最多的记录先去掉了,这样快点呵呵,只能这么理解了!!!
      

  3.   

    顶: creazyfish(梳分头的鱼) 
    条件的位置也可以快么?以前一直不知道。
      

  4.   

    http://eschool.cyu.edu.tw/mscourse/access/ch11/ch11-3.htm
      

  5.   

    但是他说的是选出来,并没有说选那些记录
    我认为select 字段1,字段2的不正确
    如果他给我具体要查哪些记录,我会这样写,没有给具体,只能这样了
    一般来说 60岁以上是会少一点
    但是他问的是有么有更简单的写法,我说第一种简单,但是他又说不行
    怪异了
    不过我同意把age>60调换过来
    只是这样写不是简单啊,是效率问题啊,是不是我没有听懂……
    总之被鄙视
      

  6.   

    那你就来个最直接的,他要的是人,得到姓名给它就得了。 
    select 姓名 from country_population where sex=1 and age>60
      

  7.   

    如果sex=0表示女性的话查询条件可以写成where sex*age>60
      

  8.   

    可以试试这样:Select * from (Select * from country_population where sex = 1) as a where age > 60
      

  9.   

    上面全是SB,剩余的事情是数据库自己优化的,你再怎么改SQL都一样
      

  10.   

    昏,在and的运算中.
    sex=1 and age>60和age>60 and sex=1是一样的. 
      

  11.   

    tiaoci(我挑刺,我快乐)希望讨论一个问题的时候不要出口伤人,我们只是讨论!不要破坏这种气氛!!!
      

  12.   

    to tiaoci(我挑刺,我快乐): 大家一起讨论时,的确要注意文明用语。
      

  13.   

    to route2:
      引用你的贴子 “如果sex=0表示女性的话查询条件可以写成where sex*age>60”
    ------------------------------------------------------------------------------
    运行的快慢与索引的优化有很大的关系。 
    if (列sex与age已单独建立索引)
      where sex=1 and age>60 较快
    else
      where sex*age>60 较快
    end if
      

  14.   

    creazyfish(梳分头的鱼) 有理,我认为。但同样支持“数据库自己优化”的观点(这个老师给我们说过)。但如果硬是说有更高效率的方法的话只能假设数据库是白痴不自动优化了。唉,其实你该在面试结束后就问到答案,甚至在面试当中要求给点提示或暗示。这在很多大公司的面试中是允许的。
      

  15.   

    对了,昨天看见MSN上一朋友的名字送你:不要怕被人BS,要做个NB的人
      

  16.   

    sex*age>60这种方法不可取。这样数据库没办法进行优化,它得把表中的每条记录都去计算一遍。
      

  17.   

    sql还真的是够大家好好学的拉,有没有什么好的书推荐一下
    THANK YOU
      

  18.   

    一楼正确另一种优化:
    select * from country_population where sex=1
    union 
    select * from country_population where age>60还有一种:
    select * from country_population with(index(索引)) where sex=1 and age>60别忘了给帖子加分哦~~!!!
      

  19.   

    to debugxp:select * from country_population where sex=1
    union 
    select * from country_population where age>60这种组合语句所得的结果与 select * from country_population where sex=1 and age>60 是不同的。
      

  20.   

    //
    select * from country_population where sex=1
    union 
    select * from country_population where age>60帅哥,这样写肯定是不对的
    你写一个出来,记录肯定多n倍莫依,你可以说说你是怎么写的吗?(就是那个工程师告诉你题目的时候^_^)
      

  21.   

    不好意思,看错了,汗
    如果是or就可以。
    比如: select * from country_population where sex=1 or age>60