MySQL Forums
Forum List  »  German

Re: Versteht SQL Ausgabe nicht!!
Posted by: Thomas Wiedmann
Date: October 05, 2010 01:03PM

Der Grund ist die für mich die "eigentlich ungültige" SQL-Abfrage (siehe GROUP BY)

SELECT l_datum,
       COUNT(*) AS anzahl
  FROM leihe
  GROUP BY MONTH(l_datum)
  ORDER BY MONTH(l_datum)

Es wird nach MONTH(l_datum) gruppiert, was als Ergebnis immer "01" liefert und dann soll die Spalte "l_datum" ausgegeben werden. Fragt sich nur von welchem Datensatz. Dies wird nicht definiert und deshalb nutzt MySQL intern eine willkürliche - aber immer identische - Sortierung (Eventuell die Reihenfolge, wie die Datensätze eingefügt sind).


Eigentlich korrekt wäre z. B. folgende Abfrage:

SELECT MONTH(l_datum), MIN(l_datum) AS von, MAX(l_datum) AS bis,  COUNT(*) AS anzahl
  FROM leihe
  GROUP BY MONTH(l_datum)
  ORDER BY MONTH(l_datum)

Wäre das eine Antwort auf Deine Frage?

Grüße
Thomas

Options: ReplyQuote


Subject
Views
Written By
Posted
1745
September 28, 2010 10:22AM
Re: Versteht SQL Ausgabe nicht!!
1040
October 05, 2010 01:03PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.