关于笛卡尔积陷阱的实例

Posted liqu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于笛卡尔积陷阱的实例相关的知识,希望对你有一定的参考价值。

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

/*当我们采用以下语句进行查询的时候,得到的结果远超过我们需要的量。*/
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‘);

 
























































































以上是关于关于笛卡尔积陷阱的实例的主要内容,如果未能解决你的问题,请参考以下文章

笛卡尔积

数据库笛卡尔积

一组python的幂集和笛卡尔积

mysql-多表联查(实例)

什么是笛卡尔积?

笛卡尔积请具体解释一下.