There are some limitations:
RETURNING id
)
INSERT INTO tags (name, color, groupid)
SELECT 'test', '000000', 56
WHERE NOT EXISTS (
SELECT text FROM tags WHERE name = 'test' AND groupid = 56
)
RETURNING id
This works - until to the point where i need to get existing id aswell. With this i only get the inserted id. Is it possible to return the value of SELECT statement if it doesn't insert?
UPDATE:
DO $$
BEGIN
IF NOT EXISTS (
SELECT text FROM tags WHERE name = 'test' AND groupid = 56
)
THEN
INSERT INTO tags (name, color, groupid)
VALUES ('test', '000000', 56) RETURNING id;
ELSE
RETURN SELECT text FROM tags WHERE name = 'test' AND groupid = 56;
END IF;
END
$$
Was testing with this kind of format - however this ends with a few errors:
RETURN cannot have a parameter in function returning void
There are three ways you can perform an “insert if not exists” query in MySQL: Using the INSERT IGNORE statement. Using the ON DUPLICATE KEY UPDATE clause. Or using the REPLACE statement.
Get ID of The Last Inserted RecordIf we perform an INSERT or UPDATE on a table with an AUTO_INCREMENT field, we can get the ID of the last inserted/updated record immediately.
Another option (still keeping the unique constraint) would be to make a more complicated SQL insert statement that inserts only if not existing, you may google "sql insert if not exist" to find some examples... Show activity on this post. You need to get the appropriate record from the ResultSet e.g.
You can do this using a CTE.
The info cte has the source data, so replace the values there with your placeholders for PHP.
The CTE will return the results from the first half of the union if an existing record exists (so will show old id), and the second half if an insert was performed (so will show the new id).
WITH info (name, color, groupid) AS
(values('test','000000',56)),
trial AS (
INSERT INTO tags(name, color, groupid)
SELECT info.name, info.color, info.groupid
FROM info
WHERE NOT EXISTS (
SELECT * FROM tags t2
WHERE t2.name = info.name AND t2.groupid= info.groupid)
RETURNING id)
SELECT tags.id
FROM tags
INNER JOIN info ON tags.name = info.name AND tags.groupid= info.groupid
UNION ALL
SELECT trial.id
FROM trial;
SQL Fiddle example: http://sqlfiddle.com/#!15/a7b0f/2
Postgres manual page for using CTEs
http://www.postgresql.org/docs/9.1/static/queries-with.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With