我有一个以下的表:
用户名 访问日期
============
aaa 2007-01-01
bbb 2007-06-10
aaa 2007-05-23
aaa 2007-06-23
我要查出在5月和6月都有访问日期的用户,
我的SQL语句是:
select 用户名 from 表1 where 5月1号 <访问日期 <5月31号 and 用户名 in ( select 用户名 from 表1 where 6月1号 <访问日期 <6月31号)
但这样的运算量太大,求一个更简单的SQL语句。谢谢!
解答如下:
解答一:
select 用户名, count(distinct substr(访问日期, 1, 7))
from 表1
where substr(访问日期, 1, 7) in ( '2007-05 ', '2007-06 ')
group by 用户名
having count(distinct substr(访问日期, 1, 7)) > 1
解答二(待考证):
select * from tablename where extract(month from 访问日期) = 5 and extract(month from 访问日期) = 6
select * from tablename where extract(month from 访问日期) in (5,6)