ray Posted August 2, 2010 Report Posted August 2, 2010 Pieņemsim, ka man ir jaunumu tabula (id, date, title, author) un autoru tabula (id, name). No šīm tabulām vajadzētu izgūt datus, par to cik katrs autors katrā mēnesī ir uzrakstījis rakstus. Tabulas izkārtojums būtu matrica, ka augšā ir mēnesis (+ gads), bet kreisajā kolonnā autora vārds (name) un attiecīgi pret vārdu un mēnesi skaits (cik raksti šajā mēnesī attiecīgajam autoram). Kā kaut ko šādu varētu realizēt? Quote
krikulis Posted August 2, 2010 Report Posted August 2, 2010 Ja pareizi sapratu SELECT count(*), author, MONTH(date) FROM news n INNER JOIN authors a ON n.author_id = a.id GROUP BY month(date), author_id; struktūra un testa dati Quote
php newbie Posted August 3, 2010 Report Posted August 3, 2010 (edited) izskatās ka viņam vajag crosstab http://www.jasny.net/articles/creating-a-cross-tab-in-mysql/ ja ir definēts mēnešu skaits nav tik traki Edited August 3, 2010 by php newbie Quote
Rich Bitch Posted August 3, 2010 Report Posted August 3, 2010 (edited) http://www.webmasterworld.com/forum88/5623.htm Edited August 3, 2010 by Rich Bitch Quote
ray Posted August 3, 2010 Author Report Posted August 3, 2010 (edited) Paldies! Edited August 3, 2010 by ray Quote
php newbie Posted August 3, 2010 Report Posted August 3, 2010 http://saveabend.blogspot.com/2006/10/mysql-crosstabs.html varētu sanākt kaut kas tāds(nav testēts): SELECT autori.name as autors, SUM(IF ( MONTH(jaunumi.date) = '1', 1, 0 )) as janvaris_2010 SUM(IF ( MONTH(jaunumi.date) = '2', 1, 0 )) as februaris_2010 SUM(IF ( MONTH(jaunumi.date) = '3', 1, 0 )) as marts_2010 FROM jaunumi JOIN autori ON jaunumi.author = autori.id GROUP BY (autori.name) WHERE YEAR(jaunumi.date) = 2010 Quote
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.