一條很 巧妙的 SQL 語句

ysjxjf發表於2008-11-07
From : http://www.itpub.net/viewthread.php?tid=1083104&page=1#pid12057097[@more@]
關於取連續資料請教,謝謝!

drop table test;
create table test (id1 int,id2 int ,id3 int);

insert into test (id1 ,id2,id3) values (1,45,89);
insert into test (id1 ,id2,id3) values (2,45,89);
insert into test (id1 ,id2,id3) values (3,45,89);
insert into test (id1 ,id2,id3) values (8,45,89);
insert into test (id1 ,id2,id3) values (12,45,89);
insert into test (id1 ,id2,id3) values (36,45,89);
insert into test (id1 ,id2,id3) values (22,45,89);
insert into test (id1 ,id2,id3) values (23,45,89);
insert into test (id1 ,id2,id3) values (89,45,89);
insert into test (id1 ,id2,id3) values (90,45,89);
insert into test (id1 ,id2,id3) values (91,45,89);
insert into test (id1 ,id2,id3) values (92,45,89);

commit;


我要的查詢結果就是查出

(1,45,89);
(2,45,89);
(3,45,89);
(22,45,89);
(23,45,89);
(89,45,89);
(90,45,89);
(91,45,89);
(92,45,89);

這些記錄就好了
select id1,id2,id3 from test where id1 in ((select id1-1 from test) union (select id1+1 from test));

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/66634/viewspace-1013082/,如需轉載,請註明出處,否則將追究法律責任。

相關文章