以下过程返回所有select语句,即使是将数据放入变量的语句
我之前的经验是使用mssql,这是我在mysql中的第一个程序
我只想要最终选择的结果,即select * from temp_cust_value;
任何帮助解决这个问题将不胜感激。
BEGIN
create TEMPORARY TABLE temp_cust_value(id int(11) NOT NULL AUTO_INCREMENT,
customerid int, customername varchar(40), spent decimal(30,3),PRIMARY KEY (`id`));
insert into temp_cust_value(customerid,customername,spent)
select t2.customerid,customer_name, sum(price) as Totalcost
from Transaction t
left join
(select o.`OName` as customer_name,c.`CustomerID`,c.`CustomerType` from Customer c
left join Organization o on c.`CustomerID`=o.`CustomerID` where c.`CustomerType`=1
union all
select i.`IName` as customer_name,c1.`CustomerID`,c1.`CustomerType` from Customer c1
left join `Individual` i on i.`CustomerID`=`c1`.`CustomerID` where `c1`.`CustomerType`=0) t2
on t.customerid=t2.customerid
Group by t.customerid,customer_name order by sum(price) desc ;
#select * from temp_cust_value;
set @cnt=1;
select @rowcount := MAX(typeid) from `Type`;
#select @rowcount;
/*select @rowcount := MAX(typeid) from `Type` ;
#set tempquery= 'ALTER TABLE temp_cust_value ADD';
#select @rowcount;
set @rowcount=4;*/
while @cnt<=@rowcount do
select t.`TypeName` into @typename1 from `Type` t where t.`TypeID`=@cnt and t.`TypeName` is not NULL;
#select @rowcount;
/*select @typename1 := t.`TypeName` from `Type` t where t.`TypeID`=cnt and t.`TypeName` is not NULL;*/
set @sql1 := concat("ALTER TABLE temp_cust_value ADD ",@typename1," int DEFAULT 0;");
#select @sql1;
PREPARE stmt3 FROM @sql1;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
set @cnt=@cnt+1;
end while;
#select * from temp_cust_value;
create temporary table gallery_customer_type_money(id int(11) NOT NULL AUTO_INCREMENT,
customerid int, typeid int, money int,PRIMARY KEY (`id`));
insert into gallery_customer_type_money(customerid, typeid,money)
select c.`CustomerID`,t2.`TypeID`,sum(p.`price`) from `Customer` c
inner join `Transaction` t on t.`CustomerID`=c.`CustomerID`
left join `Piece` p on p.`InvoiceNumber`=t.`InvoiceNumber`
left join `Type` t2 on t2.`TypeID`=p.`TypeID`
#where t2.`TypeID` IS NOT NULL
group by c.`CustomerID`,t2.`TypeID`;
#order by c.`CustomerID`,sum(p.`price`) desc ;
#select * from gallery_customer_type_money;
set @cnt1 := 1;
#set @rowcount1=1;
select @rowcount1 := MAX(id) from `gallery_customer_type_money`;
#select @rowcount1;
while @cnt1<=@rowcount1 do
select @a := customerid, @b := typeid, @c := money from gallery_customer_type_money where id=@cnt1;
#select @typedesc := t.`TypeName` from `Type` t where t.`TypeID`=@b and t.`TypeName` is not NULL;
select t.`TypeName` into @typedesc from `Type` t where t.`TypeID`=@b and t.`TypeName` is not NULL;
set @sql2 := concat("update temp_cust_value set ",@typedesc," =",@c," where customerid=",@a,";");
#select @sql2;
PREPARE stmt3 FROM @sql2;
EXECUTE stmt3;
DEALLOCATE PREPARE stmt3;
set @cnt1=@cnt1+1;
end while;
select * from temp_cust_value;
drop table temp_cust_value;
drop table gallery_customer_type_money;
END
答案 0 :(得分:0)
行。这解决了。我使用set或select into来解决这个问题。