我有一个需求,需要从另一张表中获取一列数据,并将该列数据与其他数据插入到另一个表中。
例如:
如果cust_id='11',那么我需要从cust表中获取cust_code(假设它返回cust_code='ABCD'),然后将该cust_code与其他数据一起插入到table_1中,如下所示:
WITH get_cust_code_for_cust_id AS (
SELECT cust_code FROM cust WHERE cust_id=11
)
INSERT INTO public.table_1(
cust_code, issue, status, created_on)
VALUES (SELECT cust_code FROM get_cust_code_for_cust_id, 'New Issue', 'Open', current_timestamp)
但是因为我们还没有调用get_cust_code_for_cust_id
查询,所以这个查询不起作用。
我的首选是一些带有WITH
子句的查询语句,但是任何其他答案也会被赞赏。