首先知道迪卡尔积是怎么算的先 比如 a 表 col 1 2 b表 col 3 4 ---------------------------------------------------------------- -- Author :fredrickhu(小F,向高手学习) -- Date :2010-03-16 19:38:50 -- Version: -- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86) -- Nov 24 2008 13:01:59 -- Copyright (c) 1988-2005 Microsoft Corporation -- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3) -- ---------------------------------------------------------------- --> 测试数据:[a] if object_id('[a]') is not null drop table [a] go create table [a]([col] int) insert [a] select 1 union all select 2 --> 测试数据:[b] if object_id('[b]') is not null drop table [b] go create table [b]([col] int) insert [b] select 3 union all select 4 --------------开始查询--------------------------select * from [a] cross join b----------------结果---------------------------- /* col col ----------- ----------- 1 3 2 3 1 4 2 4(4 行受影响) */
一个表A 有M行数据 表B 有N行数据
做笛卡尔积不就是 M*N除法的逆运算就是乘法
比如
a 表
col
1
2
b表
col
3
4
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2010-03-16 19:38:50
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
--
----------------------------------------------------------------
--> 测试数据:[a]
if object_id('[a]') is not null drop table [a]
go
create table [a]([col] int)
insert [a]
select 1 union all
select 2
--> 测试数据:[b]
if object_id('[b]') is not null drop table [b]
go
create table [b]([col] int)
insert [b]
select 3 union all
select 4
--------------开始查询--------------------------select * from [a] cross join b----------------结果----------------------------
/* col col
----------- -----------
1 3
2 3
1 4
2 4(4 行受影响)
*/
那么表C除以表A,就得到和表B中一样的2条记录了.就是这个道理.哈哈