mysql - Creating new items based on sort values -


i have situation i'm building new list based on sort values on column.

for example table: products id, name, sort_order, expiration_date

table: lists id, product_id

let's want have 15 products in lists table , in order. however, products expire, , when happens being removed lists table, need add in new products based on "next in line" sort_order (the last sort_order id in lists + 1, if exists, if not, start over).

i hope makes sense...

but question is, there way handle in 1 query?

right now, here's how it:

    query 1: select count(*), sort_order lists order sort_order desc  if($count < 15){     $difference = 15 - $count;     for($c = $count; $c >= 1; $c -=1){         query 2: select id products sort_order = $so + 1 limit 1         if(results = 0){              query 3: select id products order sort_order asc limit 1               query 4: insert (id) lists          }else{               query 5: insert (id) lists          }     } } 

just seems lot of queries simple task.... suggestions huge!

thank you!

hmmmz, (i'm not entirely clear on process, point).

  $start_sort_order = n;//whatever integer is.     insert lists (id)   select id products    order       sort_order < $start_sort_order, -- sort_order > $start_order == 0 => earlier,                                       -- sort_order < $start_order == 1 => @ end      sort_order                      -- afterwards standard (so result                                       -- : 89,90,93,96,97,99,4,5,6,7, etc...   limit 15; 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -