使用子查询和序列插入 sql 语句
Insert sql statement with a subquery and sequence
我有一个 insert into 语句,这个语句将有一个子查询,它从中获取所有信息。我只有一个问题,我必须使用我作为序列创建的主键索引。我只是不知道如何插入带有子查询的序列。任何帮助将非常感激。
目前 insert into 语句不起作用,但这是我目前所拥有的。
INSERT INTO data_plan_demand(data_demand_id, data_plan_name,product_demand,data_plan_inf)
VALUES( seq_data_demand_id2.nextval ,
(SELECT d.name, COUNT(u.data_id) AS product_demands,
d.information AS dataplan_information
FROM users u, data_plans d
WHERE u.data_id = d.data_plan_id
GROUP BY d.name,d.information));
您可以将序列放入 select,简单易行。 (编辑,糟糕,抱歉,错过了你正在分组)
INSERT INTO data_plan_demand(data_demand_id, data_plan_name,product_demand,data_plan_inf)
select seq_data_demand_id2.nextval, ss.name, ss.product_demands, ss.dataplan_information
from(SELECT d.name, COUNT(u.data_id) AS product_demands,
d.information AS dataplan_information
FROM users u, data_plans d
WHERE u.data_id = d.data_plan_id
GROUP BY d.name,d.information) ss;
我有一个 insert into 语句,这个语句将有一个子查询,它从中获取所有信息。我只有一个问题,我必须使用我作为序列创建的主键索引。我只是不知道如何插入带有子查询的序列。任何帮助将非常感激。 目前 insert into 语句不起作用,但这是我目前所拥有的。
INSERT INTO data_plan_demand(data_demand_id, data_plan_name,product_demand,data_plan_inf)
VALUES( seq_data_demand_id2.nextval ,
(SELECT d.name, COUNT(u.data_id) AS product_demands,
d.information AS dataplan_information
FROM users u, data_plans d
WHERE u.data_id = d.data_plan_id
GROUP BY d.name,d.information));
您可以将序列放入 select,简单易行。 (编辑,糟糕,抱歉,错过了你正在分组)
INSERT INTO data_plan_demand(data_demand_id, data_plan_name,product_demand,data_plan_inf)
select seq_data_demand_id2.nextval, ss.name, ss.product_demands, ss.dataplan_information
from(SELECT d.name, COUNT(u.data_id) AS product_demands,
d.information AS dataplan_information
FROM users u, data_plans d
WHERE u.data_id = d.data_plan_id
GROUP BY d.name,d.information) ss;