2016-06-09 6 views
0

Ich weiß nicht, warum die folgende Abfrage funktioniert perfekt in MySQL, aber nicht in PostgreSQL.SQL COUNT (*) funktioniert nicht in PostgreSQL, aber in MySQL

SELECT MAX(Anzahl) max_cnt, PID 
FROM (
SELECT COUNT(*) Anzahl, PID, postID 
FROM PersonLikesPost 
GROUP BY (postID) 
ORDER BY Anzahl DESC) as d 

PostgreSQL sagt, dass ich die PID in der GROUP BY-Klausel brauche. Aber dann bekomme ich ein anderes Ergebnis.

Grüße.

+3

Es funktioniert in MySQL, weil es standardmäßig nach inkohärenten Ergebnissen fragt. Korrigieren Sie die Abfrage und es funktioniert in jedem DBMS. –

Antwort

2

dies möglich ist, in mysql aus folgendem Grund

MySQL extends the standard SQL use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL. You can use this feature to get better performance by avoiding unnecessary column sorting and grouping. However, this is useful primarily when all values in each nonaggregated column not named in the GROUP BY are the same for each group. The server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate. Furthermore, the selection of values from each group cannot be influenced by adding an ORDER BY clause. Result set sorting occurs after values have been chosen, and ORDER BY does not affect which values within each group the server chooses.

haben einen Blick auf die Dokumentation in diesem Link https://dev.mysql.com/doc/refman/5.5/en/group-by-handling.html

Andere DBMS die Auswahl nach so nicht erweitern die Standard-SQL Verwendung von GROUP können Sie Liste in anderen DBMS kann nur auf aggregierte Spalten

dh in anderen DBMS diese Abfrage

0 siehe

haben in diesem, das hilft

SELECT COUNT(*) Anzahl, PID, postID 
    FROM PersonLikesPost 
    GROUP BY Anzahl, PID, postID 

Hoffnung übersetzt werden.

+0

Sybase hat auch eine ähnliche Erweiterung zum Standard ... – Shadow

+0

Diese Abfrage funktioniert auch nicht. Jetzt PostgreSQL sagt: Fehler: Aggregierte Funktionen sind in GROUP BY nicht erlaubt. – wir12flb

Verwandte Themen