mysql quote of the day query: cope with missing id's -


i managed create following "quote of day" script. check www.kakivi.de can see on bottom right "kindersprüche". present query running fine long id's consecutive. when 1 or more id's missing (because records have been deleted), script nor return quote specific id not there. question: how can make belowmentioned script tö "skip" id's missing? tips , suggestions in advance.

select id, spruch, vorname, nachname, datum_eintrag kindersprueche  id=`datediff`(curdate(),'2015-02-22') mod (select count(*)  kindersprueche) 

you this:

select id, spruch, vorname, nachname, datum_eintrag kindersprueche cross join      (select max(id) maxid kindersprueche) n id >= datediff(curdate(), '2015-02-22') mod (maxid + 1) order id limit 1; 

note changed condition use max(id) rather count(*). moved from clause, easier read query.

edit:

i think way fix add sequential column quotes. can in query as:

select id, spruch, vorname, nachname, datum_eintrag (select k.*, (@rn := @rn + 1) seqnum       kindersprueche cross join (select @rn := 0) params      ) k cross join      (select count(*) cnt kindersprueche) n id >= datediff(curdate(), '2015-02-22') mod (cnt + 1) order id limit 1; 

other ways solve this:

  • add column sequential (perhaps auto-incremented) table , set there no gaps.
  • use offset along limit values. however, need set in application layer, rather in query.

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

jquery - javascript onscroll fade same class but with different div -