php - Incrementing a row in a column from an empty column -


i have question regarding incrementing column in table. trying achieve is,

i using php program when user press button every row inserted do_no incremented 1 in every row insertion.

what have sql command is,

insert delivery (packing_no, do_no) values ('packing1', ((select max(do_no) delivery)+1)) 

packing_no inserted based on text input

the problem is,

  1. when table empty, do_no not updated. means keeps emptied.
  2. i guess not efficient way implement kind of problem.

anybody can suggest me better way solve ?

thanks.

you can use case condition , not see other way achieve this.

declare varmax(int) set varmax = (select max(do_no) delivery);  insert delivery (packing_no, do_no)  values ('packing1', (case when varmax null 1 else (varmax +1)) 

or simplify, can check against row count well

set varmax = (select count(do_no) delivery); 

further simplifying it, pointed in other answer, use isnull

insert delivery (packing_no, do_no)  values ('packing1', (select isnull(max(do_no),0)+1 delivery)) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -