I think the problem is that control-flow logic such as the IF
statement (NOT if()
the function) is allowed only in programming blocks -- stored procedures, functions, and trigger.
You can do what you want with a query such as:
INSERT INTO innehall (Produkter, typ)
SELECT Produkter, typ
FROM (SELECT 'kyckling' as Produkter, 'Sallader' as typ) x
WHERE NOT EXISTS (SELECT 1
FROM innehall i
WHERE i.Produkter = x.Produkter AND i.typ = x.typ
);
But the better approach is to let the database ensure uniqueness rather than you having to do this. So:
alter table innehall add constraint unq_innehall_produkter_typ
unique (produkter, typ);
And then phrase the insert like this:
insert into innehall (produkter, typ)
values (kyckling', 'Sallader')
on duplicate key update typ = values(typ);
This attempts the update. If the unique constraint is violated, then it ignores the error.