Norman
Norman

Reputation: 6365

Why is IFNULL() not working here

I'm checking if a column value is blank because I need to add 1 to it. This is actually a follow up of my question here

My Previous Question

I'm doing

INSERT INTO posts (post_user_id, gen_id)  SELECT 1, IFNULL((MAX(gen_id)+1 FROM
posts),0);

What's wrong here?

I keep getting some kind of error.

Upvotes: 0

Views: 9028

Answers (2)

SRIRAM
SRIRAM

Reputation: 1888

try case

INSERT INTO posts (post_user_id, gen_id) values 
SELECT 1,
   case when gen_id IS null then (select MAX(gen_id) from posts)
   else 0 
   end

Upvotes: 1

John Woo
John Woo

Reputation: 263723

try substituting it with COALESCE

INSERT INTO posts (post_user_id, gen_id) 
SELECT 1, COALESCE((MAX(gen_id)+1) ,0) 
FROM posts

or IFNULL

INSERT INTO posts (post_user_id, gen_id) 
SELECT 1, IFNULL((MAX(gen_id)+1) ,0) 
FROM posts

Upvotes: 4

Related Questions