I have the following query in MySQL. This inserts a new item to a list where the position of the new item is set to 1 greater than the current maximum position.
INSERT INTO items
SET item_name="apple",
position=(SELECT MAX(i.position)+1 FROM items i WHERE i.list_id=12),
list_id=12
This works, but for position, I want to insert a 0 if the SELECT statement returns an empty set. Is this possible to do in one query (so that I can avoid using transactions)?