码迷,mamicode.com
首页 > 其他好文 > 详细

关于笛卡尔积陷阱的实例

时间:2018-10-05 17:26:36      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:sts   select   insert   tab   sel   sele   超过   rtt   drop   

下面的代码演示了笛卡尔积陷阱的相关实例。

/*当我们采用以下语句进行查询的时候,得到的结果远超过我们需要的量。*/
select a.userID,B.username,A.value,A.month from
cartTableA A, cartTableB B
where A.userID = B.userID

/*甚至我们采用以下的语句的时候,也仍然得不到我们想要的结果。*/
select a.userID,B.username,A.value,A.month from
cartTableA A, cartTableB B
where A.userID = B.userID and A.month = B.month

/*只有采用以下语句才能得到我们想要查看的正确结果*/

select distinct a.userID,B.username,A.value,A.month from
cartTableA A, cartTableB B
where A.userID = B.userID and A.month = B.month

 


drop table if exists Jinhua_Electric.cartTableA ;

create table Jinhua_Electric.cartTableA(
userID char(10),
month char(10),
value decimal(8,2),
DT char(8)
);

INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180901‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180902‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180903‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180904‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180905‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180906‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180907‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180908‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180909‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180910‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180911‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180912‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180913‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201808‘, 12.333 ,‘20180914‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180901‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180902‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180903‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180904‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180905‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180906‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180907‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180908‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180909‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180910‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180911‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180912‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180913‘);
INSERT INTO Jinhua_Electric.cartTableA(userID, month, value,DT) VALUES(‘S1001‘,‘201807‘, 52.222 ,‘20180914‘);

drop table if exists Jinhua_Electric.cartTableB;
create table Jinhua_Electric.cartTableB(
userID char(10),
username char(10),
month char(10)
);

 

INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1001‘,‘A1‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1002‘,‘A2‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1003‘,‘A3‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1004‘,‘A4‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1005‘,‘A5‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1006‘,‘A6‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1007‘,‘A7‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1008‘,‘A8‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1009‘,‘A9‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1010‘,‘A10‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1011‘,‘A11‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1012‘,‘A12‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1013‘,‘A13‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1014‘,‘A14‘,‘201808‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1001‘,‘A1‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1002‘,‘A2‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1003‘,‘A3‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1004‘,‘A4‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1005‘,‘A5‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1006‘,‘A6‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1007‘,‘A7‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1008‘,‘A8‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1009‘,‘A9‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1010‘,‘A10‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1011‘,‘A11‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1012‘,‘A12‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1013‘,‘A13‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1014‘,‘A14‘,‘201807‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1001‘,‘A1‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1002‘,‘A2‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1003‘,‘A3‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1004‘,‘A4‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1005‘,‘A5‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1006‘,‘A6‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1007‘,‘A7‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1008‘,‘A8‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1009‘,‘A9‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1010‘,‘A10‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1011‘,‘A11‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1012‘,‘A12‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1013‘,‘A13‘,‘201806‘);
INSERT INTO Jinhua_Electric.cartTableB(userID, username,month) VALUES(‘S1014‘,‘A14‘,‘201806‘);

 

关于笛卡尔积陷阱的实例

标签:sts   select   insert   tab   sel   sele   超过   rtt   drop   

原文地址:https://www.cnblogs.com/liqu/p/9745036.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!