create table "aaa1"
("oid" varchar2(50) not null,
"name" varchar2(50),
"pid" varchar2(50),
"sort" int,
"version" char(10),
primary key("oid", "version")
);commit;
delete from aaa1;
insert into "aaa1" ("oid","name","pid","sort","version") values ('0', '事項', '-1', 1, '2018-01-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('1', '事項1', '0', 1, '2018-01-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('2', '事項2', '0', 2, '2018-01-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('11', '事項11', '1', 1, '2018-01-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('12', '事項12', '1', 2, '2018-01-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('21', '事項21', '2', 1, '2018-01-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('211', '事項211', '21', 1, '2018-01-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('212', '事項212', '21', 2, '2018-01-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('0', '事項', '-1', 1, '2018-03-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('1', '事項1', '0', 1, '2018-03-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('2', '事項2', '0', 2, '2018-03-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('11', '事項11', '1', 1, '2018-03-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('12', '事項12', '1', 2, '2018-03-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('21', '事項21', '2', 1, '2018-03-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('211', '事項211', '21', 1, '2018-03-01');
insert into "aaa1" ("oid","name","pid","sort","version") values ('212', '事項212', '21', 2, '2018-03-01');
commit;
--sql1
select "oid","name","pid","sort","version"
from "aaa1"
where version = '2018-03-01'
connect by prior oid = pid and version = '2018-03-01'
start with oid = '0' and version = '2018-03-01'
order siblings by sort;
--sql2
select "oid","name","pid","sort","version"
from "aaa1"
--where version = '2018-03-01'
connect by prior oid = pid and version = '2018-03-01'
start with oid = '0' and version = '2018-03-01'
order siblings by sort;
--sql3
select "oid","name","pid","sort","version"
from "aaa1"
where version = '2018-03-01'
connect by prior oid = pid --and version = '2018-03-01'
start with oid = '0' --and version = '2018-03-01'
order siblings by sort;
--sql4
select "oid","name","pid","sort","version"
from "aaa1"
where version = '2018-03-01'
connect by prior oid = pid --and version = '2018-03-01'
start with oid = '0' and version = '2018-03-01'
order siblings by sort;
請注意,在層級表中若存在多個版本的話,只查詢某個版本資訊的情況時,若僅僅在where中書寫where會把其他version的也查詢出來,connect by和start with也需要限定查詢條件,只有sql1是正確的查詢方式
多where條件查詢
create tabletb 工廠 varchar 10 庫地 int,型別 varchar 10 屬性 varchar 10 sku varchar 10 批次 varchar 10 名稱 varchar 10 入庫時間 datetime 數量 int,重量 int 備註 varchar 10 i...
where條件拼接查詢
最近在做乙個動態拼接where條件的查詢,大概想到了以下幾種方法 1 內拼接查詢條件,sql也是寫在 內的。2 內拼接查詢條件,sql寫在儲存過程內,將 where條件作為乙個字串傳入儲存過程。這種需要在 內過濾sql注入的問題 3 查詢條件的拼接放到儲存過程內,在儲存過程內對查詢條件值進行引數化。...
關於where的條件查詢in
表示式查詢 map 欄位1 array 表示式 查詢條件1 map 欄位2 array 表示式 查詢條件2 model where map select 也支援 表示式不分大小寫,支援的查詢表示式有下面幾種,分別表示的含義是 表示式含義 eq等於 neq不等於 gt大於 egt大於等於 lt小於 e...