Marcus Marcus - 5 months ago 15
SQL Question

Insert into select not working no idea why

Is it possible to do this?

insert into cart(cartid,rowid,productname) select max(cartid)+1 from cart, 2, "hello"

i want to insert my own values of rowid and productname


This is how you phrase the query:

insert into cart (cartid, rowid, productname)
    select max(cartid) + 1, 2, 'hello'
    from cart;

However, this is the wrong way to have an auto-incremented cartid. Instead, define the column to be auto-incrementing and simply do:

insert into cart (rowid, productname)
    select 2, 'hello'
    from cart;

The database takes care of the assignment automatically.