Oracle面试题

来源:百度知道 编辑:UC知道 时间:2024/05/20 04:41:31
今天遇到的一道电信方面面试题:
A表有以下字段:month(月份),user_id(用户编号),user_state(用户状态,0为正常用户,1为非正常用户)。如果某一个用户上月为正常用户,本月为非正常用户,则成为流失用户。
查询:‘200505’年的流失用户的用户编号。
我是这么做的,能查出来,但他们公司的DBA说效率太低,太麻烦!
select userid from a a1
where time='200505' and state=1
and
(select state from a a2
where a1.userid=a2.userid
and time='200504')=0

select userid from table where user_state=1 and month=5 and userid in
(select userid from table where month=4 and user_state=0);

SELECT user_id FROM A where user_state=0 and month=5 IN (SELECT * FROM A where user_state=1 and month=4)

语句的写法往往有很多种,你要根据数据库的索引来查询,公司的DBA应该在 month 上建立了索引,在where 语句中必须用到,若没有就是全表查询,当然没效率

应该还有个年吧?