当前位置:数据库 > Oracle >>

oracle语句问题:一张user表,三个字段,id,name,time,插入记录比如:张三2007,李四2008,张三2011

现在要找出所有创建时间(time)必须包括2011年,并且在其他年份也有插入记录的name
答案:SELECT name  FROM user ;
有点不知道你想问啥... 返回time列??
其他:select * from user where ( time=2011 or name='张三') create table [user](
id int primary key identity,
[name] nvarchar(50) not null,
[time] datetime not null default(getdate())
) 
insert into [User]([name],[time]) values ('张三','2007-1-1');
insert into [User]([name],[time]) values ('张三','2011-1-1');
insert into [User]([name],[time]) values ('李四','2011-1-1');
insert into [User]([name],[time]) values ('赵五','2011-1-1');

create table #tmp(
id int primary key identity,
[name] nvarchar(50) null,
[year] int null,
[count] int default(0)
) 

insert into #tmp([name],[year],[count]) select [name],year([time]),0 from [user] group by year([time]),[name]
update #tmp set [count]=(select count(*) from [user] where [user].[name]=#tmp.[name])
select * from #tmp where [year] =2011 and [count]>1


drop table #tmp declare
cursor cur is select name,cout(*) a from user where name in (select name  from user where year(time)>=2011);
begin
for v_cur in cur loop
select * from user where name=v_cur.name and v_cur.a>=2;
end loop;
end;
/ 必须包括2011,还有其它年份。这等价于所有年份的记录了???

select name 
from user  
where to_char(time,"yyyy")="2011" select a.name 
from user   a
where to_char(a.time,"yyyy")="2011" 
exists ( select 1 from user b where  to_char(b.time,"yyyy")<>"2011"  and b.name=a.name
) 

上一个:初学orcle,希望有大大帮忙解说一下详细步骤,从登录oracle到创建表的过程
下一个:如何写一个ORACLE触发器同步两个表中的数据?

Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,