日期:2014-05-17 浏览次数:20819 次
---方法一:SQL77的方法
--drop table #a
select number as x into #a
from master..spt_values where type='p' and number between 0 and 100--把0到100的数据放入表#a
select a.x,b.x y,c.x z from #a a,#a b,#a c where 5*a.x+3*b.x+1.0*c.x/3=100 --筛选出符合要求的组合数
--方法二:ssp2009的方法
--DROP TABLE #TB
DECLARE @I INT,@J INT,@K INT
CREATE TABLE #TB(I INT,J INT,K INT)
SELECT @I=0,@J=0,@K=0
WHILE @I<100
BEGIN
WHILE @J<100
BEGIN
WHILE @K<100
BEGIN
IF(@I*5+@J*3+@K*1.0/3=100)
BEGIN
INSERT INTO #TB(I,J,K) VALUES(@I,@J,@K)
END
SET @K=@K+1
END
SELECT @J=@J+1,@K=0
END
SELECT @I=@I+1,@J=0,@K=0
END
--SELECT * FROM #TB WHERE I=0 or J=0 or K=0
------解决方案--------------------
/*声明能买的范围表*/
create table #TableNum
(x int )
declare @Num1 int
declare @Num2 int
select @Num1=0 --至少要买
select @Num2=300--最多能买
while @Num1<=@Num2
begin
insert into #TableNum(x) values(@Num1)
select @Num1=@Num1+1
end
select a.x 公鸡,b.x 母鸡,c.x 小鸡 from #TableNum a,#TableNum b,#TableNum c
where
a.x<=20
and b.x<=33
and c.x<=300
and 15*a.x+9*b.x+c.x=300
and a.x+b.x+c.x=100--和起来一共100只鸡
order by a.x,b.x
drop table #TableNum