Benutzer Diskussion:SirJective/Wartungslisten/Topkategorien

Die Abfrage

Bearbeiten

Die verwendete Abfrage benutzt die Tabellen page und categorylinks (Download), und sie erzeugt eine Handvoll weiterer Tabellen, die auch für andere Zwecke nützlich sein können. --SirJective 22:21, 3. Nov 2005 (CET)


-- Alle existierenden Kategorien
drop table if exists allcats;
create table allcats(
  cat_id int(8) unsigned not null,
  cat_title varchar(255) binary not null,
  primary key (cat_id),
  unique key (cat_title)
);
replace into allcats
select page_id, page_title
from page
where page_namespace = 14;

-- Alle Links von existierender Kategorie zu existierender Kategorie
drop table if exists catlinks;
create table catlinks(
  cat_child int(8) unsigned not null,
  cat_parent int(8) unsigned not null,
  key (cat_child),
  key (cat_parent)
);
insert into catlinks
select child.cat_id, parent.cat_id
from allcats as child, categorylinks, allcats as parent
where child.cat_id = cl_from
and parent.cat_title = cl_to;


-- Alle Kategorien, die eine Oberkategorie haben
drop table if exists nontopcats;
create table nontopcats(
  ntcat_id int(8) unsigned not null,
  primary key (ntcat_id)
);
insert into nontopcats
select distinct cat_id
from allcats, catlinks
where cat_id = cat_child;

-- Alle Kategorien, die keine Oberkategorie haben
drop table if exists topcats;
create table topcats(
  tcat_id int(8) unsigned not null,
  primary key (tcat_id)
);
insert into topcats
select cat_id
from allcats left join nontopcats on (cat_id = ntcat_id)
where ntcat_id is null;


-- Alle Top-Kategorien die Artikel oder Unterkategorien haben,
-- mit der Anzahl der Artikel und Unterkategorien
drop table if exists topcats2;
create table topcats2(
  tcat_id int(8) unsigned not null,
  num_of_articles int(8) unsigned not null,
  primary key (tcat_id)
);
insert into topcats2
select tcat_id, count(*)
from topcats, page, categorylinks
where tcat_id = page_id
and cl_to = page_title
group by tcat_id;


-- fertige Ausgabe
select concat(
 '*[[:Kategorie:',page_title,']] (',
 if(num_of_articles is null,0,num_of_articles),')')
from (topcats, page) left join topcats2
on (topcats.tcat_id = page_id and topcats.tcat_id = topcats2.tcat_id)
where topcats.tcat_id = page_id
order by num_of_articles asc, page_title;