Jackal Posted April 22, 2011 Report Share Posted April 22, 2011 (edited) Ir mysql tabula kurā glabājas notikumi. Katram notikumam ir gan sākuma datums, gan beigu datums. Ir vajadzīgs parādīt visus pašlaik aktīvos (skatoties pēc datumiem) un turpmākos notikumus. Pašlaik esmu ticis līdz šādam risinājumam: SELECT * FROM `events` WHERE active = '1' AND ( (NOW() BETWEEN `from` AND `to`) OR (`to` >= NOW() AND `from` <= `to`) ) GROUP BY `id` ORDER BY `from` ASC bet problēma ir tāda, ka notikumu sarakstu vajag izdrukāt sagrupējot pa datumiem sākot ar tekošo datumu. Ja notikums ir sācies pagātne un turpinās nakotnē, tad tas arī ir jāizdrukā pie katra datuma līdz notikuma termiņs ir beidzies. Piemēram: 22. aprīlis -------- 2011-04-20 - 2011-04-25 -------- 2011-04-21 - 2011-04-27 -------- 2011-04-22 - 2011-04-26 23. aprīlis -------- 2011-04-20 - 2011-04-25 -------- 2011-04-21 - 2011-04-27 -------- 2011-04-22 - 2011-04-26 -------- 2011-04-23 - 2011-04-25 27. aprīlis -------- 2011-04-21 - 2011-04-27 -------- 2011-04-24 - 2011-04-30 Vai ar mysql vispar šādi var sagrupēt? Ceru, ka ideju sapratāt. Edited April 22, 2011 by Jackal Quote Link to comment Share on other sites More sharing options...
basicv Posted April 23, 2011 Report Share Posted April 23, 2011 Vari mēģināt tā: SELECT * FROM (SELECT `from` as dAll FROM `events` WHERE `from`>=NOW() UNION SELECT `to` FROM `events` WHERE `to`>=NOW()) dta , (SELECT * FROM `events` WHERE (NOW() BETWEEN `from` AND `to`) OR (`to` >= NOW() AND `from` <= `to`)) dtb WHERE (dta.dAll<=dtb.`from` AND dta.dAll>=dtb.`to`) OR (dta.dAll>=dtb.`from` AND dta.dAll<=dtb.`to`) Bet, ja notikumu būs daudz, būs bremzes. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.