Reputation: 4978
I want to insert a new entry in a row only if the number of rows satisfying a condition is greater than a value.
How do I do that? My sample sql query which gives error is
INSERT INTO coupon_use (coupon, customer) VALUES (3, 4)
WHERE (SELECT count(redeem_at) from coupon_use WHERE coupon=3) <= 150;
It tells me there is error in where. How do I correct this query?
I want to insert a item in this table with values 3 and 4 only if the number of coupons there are redemed are greator than 150
My database server is postgres
Upvotes: 0
Views: 314
Reputation: 60493
You can't use VALUES and a WHERE clause.
You'll need a SELECT
insert into coupon_use(coupon, customer)
select 3, 4
from coupon_use
where coupon = 3
group by coupon -- we can group by coupon to be able to use an HAVING clause
having count(redeem_at) <= 150
Upvotes: 2