标量子查询包含多行

时间:2016-11-12 17:00:35

标签: sql h2

我正在使用H2数据库,想要移动一些数据。为此我创建了以下查询:

UPDATE CUSTOMER
SET EMAIL = SELECT service.EMAIL
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID = customer.CUSTOMER_SERVICE_ID;

当我现在在H2控制台中执行它时,我收到以下错误:

Scalar subquery contains more than one row; SQL statement:

UPDATE CUSTOMER
SET EMAIL = SELECT service.EMAIL
FROM CUSTOMER_SERVICE AS service
INNER JOIN CUSTOMER AS customer ON service.ID = customer.CUSTOMER_SERVICE_ID [90053-192] 90053/90053 (Hilfe)

这个错误告诉我什么?

编辑

我希望通过查询实现目标:

实际上每个CUSTOMER都有一个CUSTOMER_SERVICE。我只是想将COLUMN EMAILCUSTOMER_SERVICE移到CUSTOMER表。为此,我已经向用户添加了一个电子邮件列。我希望能够用我的查询来做,但显然不是。

3 个答案:

答案 0 :(得分:2)

您的查询在语法上无效(所有子查询都必须在它们周围括号)。

您缺少的是相关条款。我相信你想要:

UPDATE CUSTOMER c
    SET EMAIL = (SELECT cs.EMAIL
                 FROM CUSTOMER_SERVICE s
                 WHERE s.ID = c.CUSTOMER_SERVICE_ID
                );

我不知道这应该是什么:[90053-192] 90053/90053 (Hilfe)

答案 1 :(得分:1)

您的至少一位客户的子查询有多个电子邮件地址。

你可以... (Select top 1 serverice.email ...... (Select max(serverice.email) ...

Update Customer Set EMail=B.Email
 From  Customer A
 Join  (Select ID,max(EMail) as EMail From CUSTOMER_SERVICE Group By ID) B
   on  (A.CUSTOMER_SERVICE_ID = B.ID)

答案 2 :(得分:0)

您的选择查询返回多行。如果你不想要它,那么你需要做一些像聚合或LIMIT 1或类似的东西。