中国开发网: 论坛: 数据库: 贴子 811056
haitao
一个解 八皇后 的sql——好像无法利用多核;想改为以n条记录(n个字段)代替1条记录(nxn个字段),是不是条件可以短一些?
--该表保存结果
declare @c table (c1 tinyint, c2 tinyint, c3 tinyint, c4 tinyint, c5 tinyint, c6 tinyint, c7 tinyint, c8 tinyint, c9 tinyint, c10 tinyint, c11 tinyint, c12 tinyint, c13 tinyint, c14 tinyint, c15 tinyint, c16 tinyint, c17 tinyint, c18 tinyint, c19 tinyint, c20 tinyint, c21 tinyint, c22 tinyint, c23 tinyint, c24 tinyint, c25 tinyint, c26 tinyint, c27 tinyint, c28 tinyint, c29 tinyint, c30 tinyint, c31 tinyint, c32 tinyint, c33 tinyint, c34 tinyint, c35 tinyint, c36 tinyint, c37 tinyint, c38 tinyint, c39 tinyint, c40 tinyint, c41 tinyint, c42 tinyint, c43 tinyint, c44 tinyint, c45 tinyint, c46 tinyint, c47 tinyint, c48 tinyint, c49 tinyint, c50 tinyint, c51 tinyint, c52 tinyint, c53 tinyint, c54 tinyint, c55 tinyint, c56 tinyint, c57 tinyint, c58 tinyint, c59 tinyint, c60 tinyint, c61 tinyint, c62 tinyint, c63 tinyint, c64 tinyint)
--该表做笛卡尔积
declare @tb table(t tinyint)
insert into @tb
select 1 union
select 0

--将tb表做64次笛卡尔积,产生所有的可能类型
--根据估算,一共有2的64次方种可能,每一种可能在表c中用64个tinyint型的字段保存
--无法保存所有的可能(大约1073741824T)
--一下语句先做迪卡积,然后从结果中筛选
insert into @c
select * from
(
select tb1.t c1 ,tb2.t c2 ,tb3.t c3 ,tb4.t c4 ,tb5.t c5 ,tb6.t c6 ,tb7.t c7 ,tb8.t c8 ,tb9.t c9 ,tb10.t c10,tb11.t c11,tb12.t c12,tb13.t c13,tb14.t c14,tb15.t c15,tb16.t c16,tb17.t c17,tb18.t c18,tb19.t c19,tb20.t c20,tb21.t c21,tb22.t c22,tb23.t c23,tb24.t c24,tb25.t c25,tb26.t c26,tb27.t c27,tb28.t c28,tb29.t c29,tb30.t c30,tb31.t c31,tb32.t c32,tb33.t c33,tb34.t c34,tb35.t c35,tb36.t c36,tb37.t c37,tb38.t c38,tb39.t c39,tb40.t c40,tb41.t c41,tb42.t c42,tb43.t c43,tb44.t c44,tb45.t c45,tb46.t c46,tb47.t c47,tb48.t c48,tb49.t c49,tb50.t c50,tb51.t c51,tb52.t c52,tb53.t c53,tb54.t c54,tb55.t c55,tb56.t c56,tb57.t c57,tb58.t c58,tb59.t c59,tb60.t c60,tb61.t c61,tb62.t c62,tb63.t c63,tb64.t c64
from
@tb tb1 cross join @tb tb2 cross join @tb tb3 cross join @tb tb4 cross join @tb tb5 cross join @tb tb6 cross join @tb tb7 cross join @tb tb8 cross join @tb tb9 cross join @tb tb10 cross join @tb tb11 cross join @tb tb12 cross join @tb tb13 cross join @tb tb14 cross join @tb tb15 cross join @tb tb16 cross join @tb tb17 cross join @tb tb18 cross join @tb tb19 cross join @tb tb20 cross join @tb tb21 cross join @tb tb22 cross join @tb tb23 cross join @tb tb24 cross join @tb tb25 cross join @tb tb26 cross join @tb tb27 cross join @tb tb28 cross join @tb tb29 cross join @tb tb30 cross join @tb tb31 cross join @tb tb32 cross join @tb tb33 cross join @tb tb34 cross join @tb tb35 cross join @tb tb36 cross join @tb tb37 cross join @tb tb38 cross join @tb tb39 cross join @tb tb40 cross join @tb tb41 cross join @tb tb42 cross join @tb tb43 cross join @tb tb44 cross join @tb tb45 cross join @tb tb46 cross join @tb tb47 cross join @tb tb48 cross join @tb tb49 cross join @tb tb50 cross join @tb tb51 cross join @tb tb52 cross join @tb tb53 cross join @tb tb54 cross join @tb tb55 cross join @tb tb56 cross join @tb tb57 cross join @tb tb58 cross join @tb tb59 cross join @tb tb60 cross join @tb tb61 cross join @tb tb62 cross join @tb tb63 cross join @tb tb64
) a
where
--横
c1 +c2 +c3 +c4 +c5 +c6 +c7 +c8 in(0,1) and c9 +c10+c11+c12+c13+c14+c15+c16 in(0,1) and c17+c18+c19+c20+c21+c22+c23+c24 in(0,1) and c25+c26+c27+c28+c29+c30+c31+c32 in(0,1) and c33+c34+c35+c36+c37+c38+c39+c40 in(0,1) and c41+c42+c43+c44+c45+c46+c47+c48 in(0,1) and c49+c50+c51+c52+c53+c54+c55+c56 in(0,1) and c57+c58+c59+c60+c61+c62+c63+c64 in(0,1)
and
--竖
c1 +c9 +c17+c25+c33+c41+c49+c57 in(0,1) and c2 +c10+c18+c26+c34+c42+c50+c58 in(0,1) and c3 +c11+c19+c27+c35+c43+c51+c59 in(0,1) and c4 +c12+c20+c28+c36+c44+c52+c60 in(0,1) and c5 +c13+c21+c29+c37+c45+c53+c61 in(0,1) and c6 +c14+c22+c30+c38+c46+c54+c62 in(0,1) and c7 +c15+c23+c31+c39+c47+c55+c63 in(0,1) and c8 +c16+c24+c32+c40+c48+c56+c64 in(0,1)
and
--左斜
c1 in(0,1) and c16+c23+c30+c37+c44+c51+c58 in(0,1) and c2 +c9 in(0,1) and c24+c31+c38+c45+c52+c59 in(0,1) and c3 +c10+c17 in(0,1) and c32+c39+c46+c53+c60 in(0,1) and c4 +c11+c18+c25 in(0,1) and c40+c47+c54+c61 in(0,1) and c5 +c12+c19+c26+c33 in(0,1) and c48+c55+c62 in(0,1) and c6 +c13+c20+c27+c34+c41 in(0,1) and c56+c63 in(0,1) and c7 +c14+c21+c28+c35+c42+c49 in(0,1) and c64 in(0,1) and c8 +c15+c22+c29+c36+c43+c50+c57 in(0,1)
and
--右斜
c1 +c10+c19+c28+c37+c46+c55+c64 in(0,1) and c9 +c18+c27+c36+c45+c54+c63 in(0,1) and c2 +c11+c20+c29+c38+c47+c56 in(0,1) and c17+c26+c35+c44+c53+c62 in(0,1) and c3 +c12+c21+c30+c39+c48 in(0,1) and c25+c34+c43+c52+c61 in(0,1) and c4 +c13+c22+c31+c40 in(0,1) and c33+c42+c51+c60 in(0,1) and c5 +c14+c23+c32 in(0,1) and c41+c50+c59 in(0,1) and c6 +c15+c24 in(0,1) and c49+c58 in(0,1) and c7 +c16 in(0,1) and c57 in(0,1) and c8 in(0,1)
and
--八皇后
c1+c2+c3+c4+c5+c6+c7+c8+c9+c10+c11+c12+c13+c14+c15+c16+c17+c18+c19+c20+c21+c22+c23+c24+c25+c26+c27+c28+c29+c30+c31+c32+c33+c34+c35+c36+c37+c38+c39+c40+c41+c42+c43+c44+c45+c46+c47+c48+c49+c50+c51+c52+c53+c54+c55+c56+c57+c58+c59+c60+c61+c62+c63+c64=8
--00:00:36 (92 行受影响)

select * from @c
我的blog:http://szhaitao.blog.hexun.com & http://www.hoolee.com/user/haitao
--以上均为泛泛之谈--
不尽牛人滚滚来,无边硬伤纷纷现 人在江湖(出来的),哪能不挨刀(总归是要的)
网络对话,歧义纷生;你以为明白了对方的话,其实呢?

您所在的IP暂时不能使用低版本的QQ,请到:http://im.qq.com/下载安装最新版的QQ,感谢您对QQ的支持和使用

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录