我有两个表如下。
产品表:
+-----+------------+-----+-------+--------+
| id | activityId | age | queue | status |
+-----+------------+-----+-------+--------+
| 100 | 2 | 0 | start | 2 |
| 101 | 3 | 0 | in | 5 |
+-----+------------+-----+-------+--------+
部门表:
+-----+------------+-------+----------+
| id | activityId | queue | exittime |
+-----+------------+-------+----------+
| 100 | 1 | new | null |
| 100 | 2 | start | null |
| 100 | 2 | start | null |
| 101 | 1 | new | null |
| 101 | 1 | new | null |
| 101 | 3 | in | null |
| 101 | 3 | in | null |
+-----+------------+-------+----------+
我正在尝试使用以下查询更新产品表年龄列。但其返回错误为 ORA-01427 单行子查询返回多于一行。
update Product pd set pd.age = (select (case when dp.exittime!= null then
(sysdate - dp.exittime)
else ( case when pd.queue = dp.queue
then (select (sysdate - dp1.entrytime) from department dp1 where pd.id = dp1.id
) else 2 END) END)
from department dp
where dp.id > 1
AND pd.id = dp.id
AND pd.status in('1','7','2','5')
AND pd.queue= dp.queue
AND pd.activityId = dp.activityId )
where exists
(select 1 from department dp
where dp.id > 1
AND pd.id = dp.id
AND pd.status in('1','7','2','5')
AND pd.queue= dp.queue
AND pd.activityId = dp.activityId );
由于部门表中的 activityId,子查询返回多个值。如何避免子查询返回多个值。