Jump to content
php.lv forumi

Notikumu kalendāra sql pieprasijums


Jackal

Recommended Posts

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 by Jackal
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...