我遇到错误"错误:子查询必须只返回一列"当我运行此查询时:
INSERT INTO details (id, object_id, detail)
(
SELECT
CASE
WHEN (SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id))
THEN
concat(SUBSTRING(main_base.id, '(\d+.\d+.)'), n.counted :: TEXT, 'A')
ELSE
concat( SUBSTRING (main_base.id, '(\d+.\d+.)'), n.counted :: TEXT)
END AS id,
main_base.object_id,
main_base.details
FROM main_base
CROSS JOIN LATERAL
generate_series(1, COALESCE ((string_to_array(main_base.id, '-')) [2] :: INT, 1)) AS n (counted)
WHERE main_base.id LIKE '%-%' AND NOT main_base.details ~ '^\.\d+|\(\.\d+\)'
);
我不知道出了什么问题。我已经阅读了一些话题,人们遇到了同样的问题,但仍然不知道如何解决它。
答案 0 :(得分:2)
我认为问题在于:
SELECT * FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)
可以返回多行,因此会导致WHEN
语句出现问题。它可以返回多行,因为子查询每次满足条件时都会返回1。
如果要根据此集合中何时存在某些记录来触发case语句,可以使用:
(SELECT COUNT(*) FROM details WHERE NOT EXISTS(SELECT 1 FROM main_base WHERE main_base.id = details.id)) > 1