![]() |
Datenbank: MySQL • Version: 4.x • Zugriff über: php-mysql-funktionen
Mehrere Counts ausführen
Hallo,
ich habe eine Tabelle namens "catlinks" mit drei Feldern, "cat_id", "is_main_cat" und "itm_id". Ich hätte jetzt gerne die Anzahl aller Datensätze mit cat_id = 'x' und is_main_cat = 1. D.h.
SQL-Code:
gibt mir die Anzahl der Datensätze für Kategorie 1. Ich müsste diese Abfrage für alle Kategorien, die ich habe, einzeln ausführen.
SELECT count(*) FROM catlinks WHERE is_main_cat=1 AND cat_id=1
Kann ich das nicht alles in einer Abfrage packen?
SQL-Code:
Ich nehm mal an, das funktioniert so nicht. Wie kann ich der DB sagen, dass sie bitte für mich durch die cat_id werte iterieren soll?
SELECT count(*) FROM catlinks WHERE is_main_cat=1 ORDER BY cat_id ASC
|
Re: Mehrere Counts ausführen
Du könntest dementsprechend Gruppieren.
|
Re: Mehrere Counts ausführen
SQL-Code:
//Edit: die cat_id zusätzlich abzufragen macht das Ganze sinniger ;)
SELECT cat_id,count(*) FROM catlinks
WHERE is_main_cat=1 GROUP BY cat_id ORDER BY cat_id ASC |
Re: Mehrere Counts ausführen
ausgezeichnet, DeddyH :thumb: danke.
|
Re: Mehrere Counts ausführen
:gruebel:
Ich will jetzt alle Kategorien zusammen mit der Anzahl der Einträge pro Kategorie holen. Ich hab das mal so verkuddelmuddelt:
SQL-Code:
Geht das so? Oder muss ich da n join fabrizieren oder sowas?
SELECT cats.id as id, cats.top as top, cats.name as name, cats.desc as desc, COUNT(links.*) as count
FROM ber_cats as cats, ber_catlinks as links WHERE links.is_main_cat=1 AND cats.id = links.cat_id GROUP BY cats.id ORDER BY cats.id ASC Tut mir leid dass ich hier solche Fragen stelle, aber ich hatte leider noch keine Zeit, mich umfassend mit SQL zu beschäftigen :( |
Re: Mehrere Counts ausführen
Gejoined hast Du beide Tabellen ja bereits in der WHERE-Klausel. Allerdings fallen mir 2 Dinge auf:
- Du verwendest Schlüsselwörter als Spaltenbezeichner und als Alias (top und desc). - Bei einer Abfrage, die Aggregatfunktionen enthält, musst Du nach allen Nicht-Aggregat-Feldern gruppieren.
SQL-Code:
GROUP BY cats.id , cats.top, cats.name, cats.desc
|
Re: Mehrere Counts ausführen
da ich noch in der designphase bin, werd ich die spaltenbezeichner schnell
mal ändern :) Aber nochmal danke für die schnelle Hilfe :thumb: EDIT:
SQL-Code:
Das funktioniert wirklich so? toll.
COUNT(links.*)
EDIT: Da fällt mir ein... ich hätt ja eigentlich noch n zweites aggregat. nämlich die datensätze in links, bei denen is_main_cat nicht 1 ist (also meistens 0 ;-)). hm. kann ich da einfach die bedingung is_main_cat=1 weglassen und zusätzlich nach is_main_cat gruppieren?
SQL-Code:
Ich nehm mal an, damit verdoppelt sich die anzahl meiner zurückgelieferten datensätze, jeweils einmal mit is_main_cat=1 und einmal mit =0.
SELECT cats.id as id, cats.parent as parent, cats.name as name, cats.info as info, COUNT(links.*) as count
FROM ber_cats as cats, ber_catlinks as links WHERE cats.id = links.cat_id GROUP BY cats.id , cats.parent, cats.name, cats.info, links.is_main_cat ORDER BY cats.id ASC Kann ich vielleicht ber_catlinks doppelt joinen?
SQL-Code:
Das is auch Blödsinn... ich hab einfach keine ahnung von SQL, wenns über ein "SELECT column FROM table WHERE id=x" hinausgeht :(
SELECT cats.id as id, cats.parent as parent, cats.name as name, cats.info as info, COUNT(mainlinks.*) as maincount, COUNT(seclinks.*) as seccount
FROM ber_cats as cats, ber_catlinks as mainlinks, ber_catlinks as seclinks WHERE cats.id = links.cat_id GROUP BY cats.id, cats.parent, cats.name, cats.info, mainlinks.is_main_cat ORDER BY cats.id ASC |
Re: Mehrere Counts ausführen
Du kannst auch einfach COUNT(*) schreiben, da es sich um einen INNER JOIN handelt.
|
Re: Mehrere Counts ausführen
okay. hört sich logisch an. und jetzt bitte mein zweites edit voller verworrener SQL-Versuche ;-)
|
Re: Mehrere Counts ausführen
Oha, das wird schon komplizierter. Wenn ich das richtig sehe, möchtest Du eine sog. Kreuztabelle erstellen. Leider hab ich im Moment nicht die Zeit, mir das ausführlich anzusehen (evtl. heute Abend), aber dafür schon mal ein
![]() |
Re: Mehrere Counts ausführen
Okay, schau ich mir an. Gut, dass ich mitm Kreuz keine Probleme hab :mrgreen:
EDIT: guuuutttt....
SQL-Code:
Damit kann ich wohl schonmal die Counts einzeln holen, nehm ich an.
SELECT COUNT(CASE [is_main_cat] WHEN 1 THEN * ELSE 0 END) as maincount, COUNT(CASE [is_main_cat] WHEN 0 THEN * ELSE 0 END) as seccount FROM ber_catlinks GROUP BY cat_id ORDER BY cat_id ASC
wenn ich das ganze jetzt auf meine query erweitere, muss ich das praktisch nur so splitten:
SQL-Code:
Werd gegebenenfalls heut abend nochmal dezent pushen, falls du bis dahin nichtgeantwortet hast ;-)
SELECT cats.id as id, cats.parent as parent, cats.name as name, cats.info as info, COUNT(CASE [is_main_cat] WHEN 1 THEN * ELSE 0 END) as maincount, COUNT(CASE [is_main_cat] WHEN 0 THEN * ELSE 0 END)
FROM ber_cats as cats, ber_catlinks as links WHERE cats.id = links.cat_id GROUP BY cats.id, cats.parent, cats.name, cats.info ORDER BY cats.id ASC EDIT: ist auch überhaupt nicht dringend. Werd da vor am sql sowieso nicht weiterarbeiten. |
Re: Mehrere Counts ausführen
Hallo,
ich habe das gerade mal unter Firebird probiert (allerdings nur mit wenigen Datensätzen):
SQL-Code:
Schau mal, ob das so passt.
select cats.id, cats.name, cats.parent, cats.info,
(select count(*) from links where links.is_main_cat = 0 and cats.id = links.cat_id) as Anz_0, (select count(*) from links where links.is_main_cat = 1 and cats.id = links.cat_id) as Anz_1 from cats left join links on cats.id = links.cat_id group by cats.id, cats.name, cats.parent, cats.info |
Alle Zeitangaben in WEZ +1. Es ist jetzt 11:37 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz