我有两张桌子(这里简化了很多):
QUADRI
<!DOCTYPE html>
<html lang="en" ng-app="weather">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Weather</title>
<link rel="stylesheet" href=".\bootstrap-4.0.0-dist\css\bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.7/angular.min.js"></script>
</head>
<body>
<header>
<nav class="navbar navbar-default">
<div class="container-fluid">
<div class="navbar-header">
<a href="/" class="navbar-brand">Weather</a>
</div>
<ul class="nav navbar-nav navbar-right">
<li><a href="#"><i class="fa fa-home"></i>Home</a></li>
</ul>
</div>
</nav>
</header>
<div class="container">
<div ng-view>
</div>
</div>
<script>
var weather = angular.module('weather', ['ngRoute', 'ngResource'])
// config
weather.config(function($routeProvider) {
$routeProvider
.when('./', {
templateUrl: 'home.htm',
controller: 'homeC'
})
.when('/forecast', {
templateUrl: 'fore.html',
controller: 'foreC'
})
})
</script>
</body>
</html>
勒
ID SBR_750 b10C TGI
---------------------
Q1 0 1 0
Q2 2 1 0
Q3 1 0 1
我希望这个结果在CELLE:
CELLANAME NEEDED READY
----------------------
SBR_750 NULL 12
b10C NULL 10
TGI NULL 5
我尝试编写存储过程但它不起作用:ERROR 1210.EXECUTE的参数不正确。
以下是代码:
CELLANAME NEEDED READY
------------------------
SBR_750 3 12
b10C 2 10
TGI 1 5
更新: 它不起作用所以我改变策略:
CREATE DEFINER=`root`@`%.zamberlan.local` PROCEDURE `AggiornaCelle`(IN nomecella varchar(15))
BEGIN
set @s='update celle set needed=(select sum(?) from quadri) where cellaname=?';
set @NC=nomecella;
prepare stmt from @s;
execute stmt using @NC;
deallocate prepare stmt;
END
受mysql, iterate through column names启发。
但是我收到错误“光标未打开...”
答案 0 :(得分:2)
您确实需要使用动态SQL来执行此操作。 Celle从quadri知道它需要的所有列,因此你可以从这个事实推动动态语句的创建。使用游标是一种很好的方式。
drop table if exists quadri;
create table quadri(ID varchar(2),SBR_750 int, b10C int, TGI int);
insert into quadri values
('Q1' , 0 , 1 , 0),
('Q2' , 2 , 1 , 0),
('Q3' , 1 , 0 , 1);
drop table if exists celle;
create table CELLE (CELLANAME varchar(20) ,NEEDED int,READY int);
insert into celle values
('SBR_750' , NULL , 12),
('b10C' , NULL , 10),
('TGI' , NULL , 5);
drop procedure if exists `AggiornaCelle`;
delimiter $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `AggiornaCelle`()
begin
DECLARE done INT DEFAULT FALSE;
declare col_name varchar(20);
declare cur1 CURSOR FOR
SELECT cellaname FROM celle ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cur1;
read_loop: loop
fetch cur1 into col_name;
if done then leave read_loop; end if;
set @sqlstr = concat('update celle set needed = (select sum(',col_name,') from quadri) where cellaname = ', char(39),col_name,char(39),';');
insert into debug_table (msg) values(@sqlstr);
prepare stmt from @sqlstr;
execute stmt ;
deallocate prepare stmt;
end loop;
close cur1;
end $$
delimiter ;
truncate table debug_table;
call `AggiornaCelle`();
select * from debug_table;
select * from celle;
MariaDB [sandbox]> select * from debug_table;
+----+------------------------------------------------------------------------------------------+------+
| id | msg | MSG2 |
+----+------------------------------------------------------------------------------------------+------+
| 1 | update celle set needed = (select sum(SBR_750) from quadri) where cellaname = 'SBR_750'; | NULL |
| 2 | update celle set needed = (select sum(b10C) from quadri) where cellaname = 'b10C'; | NULL |
| 3 | update celle set needed = (select sum(TGI) from quadri) where cellaname = 'TGI'; | NULL |
+----+------------------------------------------------------------------------------------------+------+
3 rows in set (0.00 sec)
MariaDB [sandbox]> select * from celle;
+-----------+--------+-------+
| CELLANAME | NEEDED | READY |
+-----------+--------+-------+
| SBR_750 | 3 | 12 |
| b10C | 2 | 10 |
| TGI | 1 | 5 |
+-----------+--------+-------+
3 rows in set (0.00 sec)
debug_table只存在,以便我可以检查更新语句。
答案 1 :(得分:0)
设置@ s ='更新celle set needed =(从quadri中选择sum(?))其中cellaname =?';
你应该通过两个论点。
execute stmt using @NC;
应该是类似的东西
execute stmt using @NC, @NC;
@NC
与尝试更新celle
中的行相同,与quadri
表中的列名相同。