I have three tables:
CREATE TABLE offers
(
id serial NOT NULL PRIMARY KEY,
title character varying(1000) NOT NULL DEFAULT ''::character varying
);
CREATE TABLE items
(
id serial NOT NULL PRIMARY KEY,
offer_id integer NOT NULL,
title character varying(1000) NOT NULL DEFAULT ''::character varying,
CONSTRAINT items_offer_id_fkey FOREIGN KEY (offer_id)
REFERENCES offers (id)
);
CREATE TABLE sizes
(
id serial NOT NULL PRIMARY KEY,
item_id integer NOT NULL,
title character varying(1000) NOT NULL DEFAULT ''::character varying,
CONSTRAINT sizes_item_id_fkey FOREIGN KEY (item_id)
REFERENCES items (id)
);
I have 1 offer that has 2 items. Each item has 2 sizes:
INSERT INTO offers (title) VALUES ('My Offer');
INSERT INTO items (offer_id, title) VALUES (1, 'First Item');
INSERT INTO items (offer_id, title) VALUES (1, 'Second Item');
INSERT INTO sizes (item_id, title) VALUES (1, 'First Size of Item #1');
INSERT INTO sizes (item_id, title) VALUES (1, 'Second Size of Item #1');
INSERT INTO sizes (item_id, title) VALUES (2, 'First Size of Item #2');
INSERT INTO sizes (item_id, title) VALUES (2, 'Second Size of Item #2');
Is there a way to clone an offer with all its items and sizes with a single query?
I tried to solve it with CTE, here is my SQL:
WITH tmp_offers AS (
INSERT INTO offers (title)
SELECT title FROM offers WHERE id = 1
RETURNING id
), tmp_items AS (
INSERT INTO items (offer_id, title)
(SELECT (SELECT id FROM tmp_offers), title FROM items WHERE offer_id = 1)
RETURNING id
)
INSERT INTO sizes (item_id, title)
(SELECT (SELECT id FROM tmp_items), title FROM sizes WHERE id IN (
SELECT sizes.id FROM sizes
JOIN items ON items.id = sizes.item_id
WHERE items.offer_id = 1
));
But this SQL results to an error, that I can't resolve:
ERROR: more than one row returned by a subquery used as an expression
Your help is greatly appreciated.
PS I use PostgreSQL 9.5
This should work:
WITH tmp_offers AS (
INSERT INTO offers (title)
SELECT title
FROM offers
WHERE id = 1
RETURNING id
), tmp_items AS (
INSERT INTO items (offer_id, title)
SELECT o.id, i.title
FROM items i
cross join tmp_offers o
WHERE i.offer_id = 1
order by i.id
RETURNING items.id
), numbered_new as (
select ti.id,
row_number() over (order by ti.id) as rn
from tmp_items ti
), numbered_old as (
select i.id,
row_number() over (order by i.id) as rn
from items i
WHERE i.offer_id = 1
), item_mapper as (
select n.id as new_item_id,
o.id as old_item_id
from numbered_new n
join numbered_old o on n.rn = o.rn
)
INSERT INTO sizes (item_id, title)
select im.new_item_id, s.title
from sizes s
join item_mapper im on im.old_item_id = s.item_id;
Online example: http://rextester.com/RYQUS11008
You are quite close. It is the final query that needs work:
WITH tmp_offers AS (
INSERT INTO offers (title)
SELECT title FROM offers WHERE id = 1
RETURNING id
),
tmp_items AS (
INSERT INTO items (offer_id, title)
SELECT o.id, i.title
FROM items i CROSS JOIN
(SELECT id FROM tmp_offers) o
WHERE i.offer_id = 1
RETURNING id, title
)
INSERT INTO sizes (item_id, title)
SELECT i.id, i.title
FROM tmp_items i;
The major difference here is that tmp_items
now has two columns -- and they appear to be the columns that you want for this purpose.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.