我有两张桌子: 表1具有5列(col1,col2,col3,col4,val1)和表2,表1具有5列(col1,col2,col3,col4,val2)。 1. Table1.val1不包含任何值。 2.对于Table1和Table2,col1,col2,col3和col4是相同的,它们是主键。
我想做的是,当Table1.col1 = Table2.col1和Table1.col2 = Table2.col2和Table1.col3 = Table2.col3时,用sum(Table2.val2)更新Table1.val1 Table1.col4 = Table2.col4。
我做了类似的事情:
UPDATE Table1
SET val1 = (
select t_sommevbrute.sumvalbrute from (
Select col1,col2,col3,col4,SUM(val2) sumvalbrute
From Table2
Where col3 = 2014 And col2=51
GROUP BY col1, col2, col3, col4) t_sommevbrute
WHERE Table1.col1 = t_sommevbrute.col1
and Table1.col2 = t_sommevbrute.col2
and Table1.col3 = t_sommevbrute.col3
and Table1.col4 = t_sommevbrute.col4)
但与此问题相关:Oracle SQL: Update a table with data from another table,我应该有WHERE EXISTS
条款。
任何帮助请!! 感谢。
答案 0 :(得分:1)
您可以通过以下方式执行此操作: -
使用临时表: - 首先创建临时表: -
Create table temp1 as
Select col1,col2,col3,col4,SUM(val2) as sumvalbrute
From table2
Where col3 = 3 And col2=2
GROUP BY col1, col2, col3, col4;
然后使用Temp表更新Main Table1: -
UPDATE table1 SET table1.val1 = (SELECT temp1.sumvalbrute
FROM temp1
WHERE Table1.col1 = temp1.Col1
AND Table1.col2 = temp1.Col2
AND Table1.col3 = temp1.Col3
AND Table1.col4 = temp1.Col4);
SQL小提琴: - http://sqlfiddle.com/#!4/4864d/5
使用临时表: -
UPDATE table1 SET table1.val1 = (SELECT temp1.sumvalbrute
FROM
(Select col1,col2,col3,col4,SUM(val2) as sumvalbrute
From table2
Where col3 = 3 And col2=2
GROUP BY col1, col2, col3, col4) temp1
WHERE Table1.col1 = temp1.Col1
AND Table1.col2 = temp1.Col2
AND Table1.col3 = temp1.Col3
AND Table1.col4 = temp1.Col4);
SQL小提琴: - http://sqlfiddle.com/#!4/c9286/2
答案 1 :(得分:0)
您可以大大简化查询:
UPDATE Table1
SET val1 = (select SUM(val2)
from Table2 t
where Table1.col1 = t.col1 and
Table1.col2 = t.col2 and
Table1.col3 = t.col3 and
Table1.col4 = t.col4
)
Where col3 = 2014 And col2 = 51;
你可以使用where exists
。目的是在没有匹配时阻止NULL
值。但是,我认为将where
子句拉到外面可能解决了这个问题。我注意到where
子句使用的值与相关条件相同。
答案 2 :(得分:0)
你可以试试这个:
<?php
$url = "https://www.indiegogo.com/projects/over-and-above-africa-the-solution-to-end-poaching-drones";
$timeout = 30;
$http = curl_init($url);
curl_setopt($http, CURLOPT_RETURNTRANSFER, true);
curl_setopt($http, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($http, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; fr; rv:1.9.2.13) Gecko/20101203 Firefox/3.6.13');
curl_setopt($http, CURLOPT_FRESH_CONNECT, true);
curl_setopt($http, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($http, CURLOPT_TIMEOUT, $timeout);
curl_setopt($http, CURLOPT_CONNECTTIMEOUT, $timeout);
if (preg_match('`^https://`i', $url))
{
curl_setopt($http, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($http, CURLOPT_SSL_VERIFYHOST, 0);
}
curl_setopt($http, CURLOPT_HEADER, true);
curl_setopt($http,CURLOPT_HTTPHEADER,array('Accept-Language', 'nl,en;q=0.7;en-us;q=0.3'));
$sContenu = curl_exec($http);
echo curl_getinfo($http, CURLINFO_HTTP_CODE);
curl_close($http);
echo $sContenu;
?>
答案 3 :(得分:0)
只能使用合并语句来实现这种更新:
Merge into Table1 using ( select SUM(val2) as val2, t.col1, t.col2, t.col3, t.col4
from Table2 t
group by t.col1, t.col2, t.col3, t.col4
) t -- This is the alias for table 2
on ( Table1.col1 = t.col1
and Table1.col2 = t.col2
and Table1.col3 = t.col3
and Table1.col4 = t.col4
)
when matched then
UPDATE Table1.val1 = t.val2 --HERE IS YOUR **UPDATE**
where Table1.col3 = 2004
and Table1.col2 = 51
;
commit;
请不要忘记将别名添加到表中,并在此代码后添加一个良好的COMMIT 此解决方案避免您创建临时表,并且效果很好
最诚挚的问候。