首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

挺怪的一个SQL,该怎么解决

2013-04-21 
挺怪的一个SQLTable Aa1 NVRCHAR(10),a2 NVRCHAR(10),a3 NVRCHAR(10)Table Bb1 NVRCHAR(10),b2 NVRCHAR(10

挺怪的一个SQL
Table A
a1 NVRCHAR(10),
a2 NVRCHAR(10),
a3 NVRCHAR(10)

Table B
b1 NVRCHAR(10),
b2 NVRCHAR(10),
b3 NVRCHAR(10)

SELECT *
FROM A
INNER JOIN B
ON A.a1 = '1'
WHERE B.b1 = '2'

今天看到别人写的这个SQL,这是什么用意呢
结果是笛卡尔积吗?
[解决办法]


--> 测试数据: @TableA
declare @TableA table (a1 int,a2 int,a3 varchar(1))
insert into @TableA
select 1,2,'a' union all
select 1,3,'b' union all
select 2,2,'c' union all
select 2,3,'d' union all
select 3,1,'e' union all
select 3,3,'f'

--> 测试数据: @TableB
declare @TableB table (b1 int,b2 int,b3 varchar(1))
insert into @TableB
select 1,2,'a' union all
select 1,3,'b' union all
select 2,2,'c' union all
select 2,3,'d' union all
select 3,1,'e' union all
select 3,3,'f'

select * from @TableA a inner join @TableB b
on a.a1='1' where b.b1='2'
/*
a1          a2          a3   b1          b2          b3
----------- ----------- ---- ----------- ----------- ----
1           2           a    2           2           c
1           3           b    2           2           c
1           2           a    2           3           d
1           3           b    2           3           d
*/


你看一下结果就明白了

热点排行