Skip to content

Instantly share code, notes, and snippets.

@eyalroth
Created May 8, 2018 21:18
Show Gist options
  • Save eyalroth/b0444eb48f736d4e2787b2aa2186c96d to your computer and use it in GitHub Desktop.
Save eyalroth/b0444eb48f736d4e2787b2aa2186c96d to your computer and use it in GitHub Desktop.
WITH
val (l_name,d_label ) AS
(values
(? , ?)
),
ins AS
( INSERT INTO
label (label_name, device_label)
SELECT l_name, d_label FROM val
ON CONFLICT ON CONSTRAINT label_name_unique DO NOTHING
RETURNING id ,label_name -- only the inserted ones
)
SELECT COALESCE(ins.id, label.id) AS id
FROM val
LEFT JOIN ins ON ins.label_name = val.l_name
LEFT JOIN label ON label.label_name = val.l_name ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment