From 9649cebf3f58030484dc3c4d19c6311db24a31f2 Mon Sep 17 00:00:00 2001 From: lzlk <1930069369@qq.com> Date: Tue, 27 Sep 2022 18:53:04 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-9-26\344\275\234\344\270\232.sql" | 38 +++++++++++++++++++ ...4\350\256\260 \350\247\206\345\233\276.md" | 9 +++++ 2 files changed, 47 insertions(+) create mode 100644 "07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" create mode 100644 "07 \345\273\226\346\262\273\345\205\210/\347\254\224\350\256\260/2022-9-26\347\254\224\350\256\260 \350\247\206\345\233\276.md" diff --git "a/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..bf9d61d --- /dev/null +++ "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" @@ -0,0 +1,38 @@ +create database bank + +use bank + +create table htzq( +year varchar(10), +month varchar(10), +amount decimal(13,2) +); + +insert into htzq values('1991','1',1.1); +insert into htzq values('1991','2',1.2); +insert into htzq values('1991','3',1.3); +insert into htzq values('1991','4',1.4); +insert into htzq values('1992','1',2.1); +insert into htzq values('1992','2',2.2); +insert into htzq values('1992','3',2.3); +insert into htzq values('1992','4',2.4); + +select * from htzq + +select year,sum(case month when 1 then amount end) m1, + sum(case month when 2 then amount end) m2, + sum(case month when 3 then amount end) m3, + sum(case month when 4 then amount end) m4 +from htzq group by year +; +create view h1(year,m1,m2,m3,m4) +as +select * +from htzq +pivot( +sum(amount) for month in([1],[2],[3],[4]) +)t; +--drop view h1 + +select * from h1 +; diff --git "a/07 \345\273\226\346\262\273\345\205\210/\347\254\224\350\256\260/2022-9-26\347\254\224\350\256\260 \350\247\206\345\233\276.md" "b/07 \345\273\226\346\262\273\345\205\210/\347\254\224\350\256\260/2022-9-26\347\254\224\350\256\260 \350\247\206\345\233\276.md" new file mode 100644 index 0000000..d677848 --- /dev/null +++ "b/07 \345\273\226\346\262\273\345\205\210/\347\254\224\350\256\260/2022-9-26\347\254\224\350\256\260 \350\247\206\345\233\276.md" @@ -0,0 +1,9 @@ +视图(Views)--虚拟关系(依赖于表,是临时的) + +建立视图 create view <视图名> [(<列名>[,<列名>]...)] + as 查询 +drop view 视图名 + +with check option :限制条件用于where后,用于限制 更新,增加操作增加和更新必须符合where后的条件 + +pivot:列转行 -- Gitee From 46053056f38792c59424d95d53e2325b1d98e9a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=96=E6=B2=BB=E5=85=88?= <1930069369@qq.com> Date: Tue, 27 Sep 2022 13:02:48 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2007?= =?UTF-8?q?=20=E5=BB=96=E6=B2=BB=E5=85=88/=E4=BD=9C=E4=B8=9A/2022-9-26?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-9-26\344\275\234\344\270\232.sql" | 38 ------------------- 1 file changed, 38 deletions(-) delete mode 100644 "07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" diff --git "a/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" deleted file mode 100644 index bf9d61d..0000000 --- "a/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" +++ /dev/null @@ -1,38 +0,0 @@ -create database bank - -use bank - -create table htzq( -year varchar(10), -month varchar(10), -amount decimal(13,2) -); - -insert into htzq values('1991','1',1.1); -insert into htzq values('1991','2',1.2); -insert into htzq values('1991','3',1.3); -insert into htzq values('1991','4',1.4); -insert into htzq values('1992','1',2.1); -insert into htzq values('1992','2',2.2); -insert into htzq values('1992','3',2.3); -insert into htzq values('1992','4',2.4); - -select * from htzq - -select year,sum(case month when 1 then amount end) m1, - sum(case month when 2 then amount end) m2, - sum(case month when 3 then amount end) m3, - sum(case month when 4 then amount end) m4 -from htzq group by year -; -create view h1(year,m1,m2,m3,m4) -as -select * -from htzq -pivot( -sum(amount) for month in([1],[2],[3],[4]) -)t; ---drop view h1 - -select * from h1 -; -- Gitee From a5211c90db50befd1e122b99cf8a89e1d6893231 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=96=E6=B2=BB=E5=85=88?= <1930069369@qq.com> Date: Tue, 27 Sep 2022 13:03:16 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 廖治先 <1930069369@qq.com> --- .../2022-9-26\344\275\234\344\270\232.sql" | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 "07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" diff --git "a/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..9a11eae --- /dev/null +++ "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" @@ -0,0 +1,46 @@ +--1 дͼʵֲѯп˻Ϣʾţ֤ +use BankTest +create view information(,֤,,) +as +select b.CardNo,a.AccountCode,a.RealName,b.CardMoney from AccountInfo a +inner join BankCard b on b.AccountId=a.AccountId + +select * from information +--2 ת +create database bank +use bank + +create table htzq( +year varchar(10), +month varchar(10), +amount decimal(13,2) +); + +insert into htzq values('1991','1',1.1); +insert into htzq values('1991','2',1.2); +insert into htzq values('1991','3',1.3); +insert into htzq values('1991','4',1.4); +insert into htzq values('1992','1',2.1); +insert into htzq values('1992','2',2.2); +insert into htzq values('1992','3',2.3); +insert into htzq values('1992','4',2.4); + +select * from htzq + +select year,sum(case month when 1 then amount end) m1, + sum(case month when 2 then amount end) m2, + sum(case month when 3 then amount end) m3, + sum(case month when 4 then amount end) m4 +from htzq group by year +; +create view h1(year,m1,m2,m3,m4) +as +select * +from htzq +pivot( +sum(amount) for month in([1],[2],[3],[4]) +)t; +--drop view h1 + +select * from h1 +; -- Gitee From 6a216a43a151595f6a1acb973d40662f92ddc4f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=96=E6=B2=BB=E5=85=88?= <1930069369@qq.com> Date: Tue, 27 Sep 2022 13:04:57 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2007?= =?UTF-8?q?=20=E5=BB=96=E6=B2=BB=E5=85=88/=E4=BD=9C=E4=B8=9A/2022-9-26?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A.sql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2022-9-26\344\275\234\344\270\232.sql" | 46 ------------------- 1 file changed, 46 deletions(-) delete mode 100644 "07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" diff --git "a/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" deleted file mode 100644 index 9a11eae..0000000 --- "a/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" +++ /dev/null @@ -1,46 +0,0 @@ ---1 дͼʵֲѯп˻Ϣʾţ֤ -use BankTest -create view information(,֤,,) -as -select b.CardNo,a.AccountCode,a.RealName,b.CardMoney from AccountInfo a -inner join BankCard b on b.AccountId=a.AccountId - -select * from information ---2 ת -create database bank -use bank - -create table htzq( -year varchar(10), -month varchar(10), -amount decimal(13,2) -); - -insert into htzq values('1991','1',1.1); -insert into htzq values('1991','2',1.2); -insert into htzq values('1991','3',1.3); -insert into htzq values('1991','4',1.4); -insert into htzq values('1992','1',2.1); -insert into htzq values('1992','2',2.2); -insert into htzq values('1992','3',2.3); -insert into htzq values('1992','4',2.4); - -select * from htzq - -select year,sum(case month when 1 then amount end) m1, - sum(case month when 2 then amount end) m2, - sum(case month when 3 then amount end) m3, - sum(case month when 4 then amount end) m4 -from htzq group by year -; -create view h1(year,m1,m2,m3,m4) -as -select * -from htzq -pivot( -sum(amount) for month in([1],[2],[3],[4]) -)t; ---drop view h1 - -select * from h1 -; -- Gitee From ea3260b3dbb9b49e7222fbf44cf9aca4997f84c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BB=96=E6=B2=BB=E5=85=88?= <1930069369@qq.com> Date: Tue, 27 Sep 2022 13:06:01 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=E7=AC=AC=E5=85=AD=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 廖治先 <1930069369@qq.com> --- .../2022-9-26\344\275\234\344\270\232.sql" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" diff --git "a/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" new file mode 100644 index 0000000..af07d0b --- /dev/null +++ "b/07 \345\273\226\346\262\273\345\205\210/\344\275\234\344\270\232/2022-9-26\344\275\234\344\270\232.sql" @@ -0,0 +1,48 @@ +--1 дͼʵֲѯп˻Ϣʾţ֤ +use BankTest +create view information(,֤,,) +as +select b.CardNo,a.AccountCode,a.RealName,b.CardMoney from AccountInfo a +inner join BankCard b on b.AccountId=a.AccountId + +select * from information +--2 ת +create database bank +use bank + +create table htzq( +year varchar(10), +month varchar(10), +amount decimal(13,2) +); + +insert into htzq values('1991','1',1.1); +insert into htzq values('1991','2',1.2); +insert into htzq values('1991','3',1.3); +insert into htzq values('1991','4',1.4); +insert into htzq values('1992','1',2.1); +insert into htzq values('1992','2',2.2); +insert into htzq values('1992','3',2.3); +insert into htzq values('1992','4',2.4); + +select * from htzq +--2.1 +select year,sum(case month when 1 then amount end) m1, + sum(case month when 2 then amount end) m2, + sum(case month when 3 then amount end) m3, + sum(case month when 4 then amount end) m4 +from htzq group by year +; + +--2.2 +create view h1(year,m1,m2,m3,m4) +as +select * +from htzq +pivot( +sum(amount) for month in([1],[2],[3],[4]) +)t; +--drop view h1 + +select * from h1 +; -- Gitee