【问题标题】:Update table from another sub-query table - "SQL command not properly ended"从另一个子查询表更新表 - “SQL 命令未正确结束”
【发布时间】:2018-08-04 14:16:22
【问题描述】:

我试图弄清楚如何从另一个表中更新一个表,这是 PL/SQL 中查询的结果。

这是我尝试做的:

update c
set c.customerType = n.newType
from customers c
join 
    (select 
         c.customerNumber, c.customerType, 
         case when c.customerType = 'business' and count(s.subCode) = 1 
                 then 'small business' 
                 else null  
         end as NewType
     from customers c
     join subscribers s on c.customerNumber = s.customerNumber
     group by c.customerNumber, c.customerType) n on c.customerNumber = n.customerNumber;

注意,客户表如下所示:

customerNumber   customerType
------------------------------
1                business
4                business
2                private
3                private

表 n(子查询)给出以下结果:

customerNumber   customerType   newType
1                business       null
4                business       small business
3                private        null
2                private        null

我做错了什么?

P.S 最终我想用 newType 更新 customerType 只在它不为空的地方..

谢谢

【问题讨论】:

  • Oracle 不支持update 中的from 子句,所以你的问题很不清楚。
  • 那么我如何在 oracle 中做到这一点?即如何从另一个表中更新一个表,这是子查询的结果?谢谢

标签: sql join plsql sql-update


【解决方案1】:

这样试试

UPDATE customers
SET customerType = ( SELECT N.newType 
                        FROM
                        (
                            SELECT c.customerNumber, c.customerType, CASE WHEN c.customerType = 'business' AND count(s.subCode) = 1 THEN 'small business' ELSE NULL END AS NewType
                            FROM customers c1 JOIN subscribers s ON c1.customerNumber = s.customerNumber
                            GROUP BY c1.customerNumber, c.customerType
                        ) N
                    );

【讨论】:

  • 谢谢,我尝试运行您的代码,但在最后一个 kine (AS N) 上出现错误 - 缺少右括号,但没有缺少括号..
【解决方案2】:

您使用相关子查询。不过,就您而言,我认为所有工作都可以在外部 where 子句中完成:

update customers c
    set c.customerType = 'small business'
    where c.customerType = 'business' and
          (select count(s.subcode)
           from subscribers s
           where s.customerNumber = c.customerNumber
          ) = 1;

如果客户在subscribers 表中恰好有一个subcode,这会将“企业”客户更新为“小型企业”。

【讨论】:

  • 非常感谢!我认为应该有一种更简单的方法,但这是我第一次尝试了解更新。如果您能解释为什么我的方式不起作用,我将不胜感激,即相关子查询是什么意思。谢谢
  • @Hana 。 . .您的方法不起作用,因为您的数据库的语法不支持 fromupdate。相关子查询是where 子句中的子查询。内部where 将内部from 子句中的表与外部from 子句中的表相关联。
【解决方案3】:
you to write plsql like below
=============================
   declare
     cursor c1 is
        select c.customerNumber, c.customerType, 
             case when c.customerType = 'business' and count(s.subCode) = 1 then 'small 
              business'
             else null end as NewType
        from customers c,subscribers s
        where c.customerNumber = s.customerNumber
        group by c.customerNumber, c.customerType;
begin
for cur in c1 loop
     update customers set customer_type = cur.NewType
     where customerNumber = cur.customerNumber;
     commit;
end loop;
end;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    • 1970-01-01
    相关资源
    最近更新 更多