PostgreSQL GROUP BY unterscheidet sich von MySQL?

Ich habe die Migration einige meiner MySQL-Abfragen für die PostgreSQL verwenden, Heroku. Die meisten meiner Anfragen funktionieren, aber ich halte eine ähnliche wiederkehrende Fehler, wenn ich group by:

FEHLER: Spalte "XYZ" muss in der GROUP BY-Klausel oder verwendet werden
eine Aggregatfunktion

Könnte mir jemand sagen was ich falsch mache?

MySQL, das funktioniert 100%:

SELECT `availables`.*
FROM `availables`
INNER JOIN `rooms` ON `rooms`.id = `availables`.room_id
WHERE (rooms.hotel_id = 5056 AND availables.bookdate BETWEEN '2009-11-22' AND '2009-11-24')
GROUP BY availables.bookdate
ORDER BY availables.updated_at

PostgreSQL-Fehler:

ActiveRecord::StatementInvalid: PGError: FEHLER: Spalte
"availables.id" muss in der GROUP BY-Klausel oder in einem
die Aggregat-Funktion:
WÄHLEN Sie "availables".* VON "availables" INNERE
JOIN "Zimmer" "Zimmer".id = "availables".room_id WO
(Zimmer.hotel_id = 5056 UND availables.bookdate ZWISCHEN E'2009-10-21'
UND E'2009-10-23') GROUP BY availables.bookdate, UM DURCH
availables.updated_at

Ruby code generieren der SQL:

expiration = Available.find(:all,
    :joins => [ :room ],
    :conditions => [ "rooms.hotel_id = ? AND availables.bookdate BETWEEN ? AND ?", hostel_id, date.to_s, (date+days-1).to_s ],
    :group => 'availables.bookdate',
    :order => 'availables.updated_at')  

Erwartete Ausgabe (arbeiten, MySQL-query):

+-----+-------+-------+------------+---------+---------------+---------------+ 
| id | Preis | spots | bookdate | room_id | created_at | updated_at | 
+-----+-------+-------+------------+---------+---------------+---------------+ 
| 414 | 38.0 | 1 | 2009-11-22 | 1762 | 2009-11-20... | 2009-11-20... | 
| 415 | 38.0 | 1 | 2009-11-23 | 1762 | 2009-11-20... | 2009-11-20... | 
| 416 | 38.0 | 2 | 2009-11-24 | 1762 | 2009-11-20... | 2009-11-20... | 
+-----+-------+-------+------------+---------+---------------+---------------+ 
3 rows in set 
sooo... ich wäre besser bedient mit der distinct-Funktion auf bookdate? Wenn ich das täte, würde ich noch die group by-Klausel?
DISTINCT ist langsamer als GROUP BY. So sollten Sie vorsichtig sein und lieber eine GROUP BY Lösung, wenn es möglich ist.

InformationsquelleAutor holden | 2009-11-20

Schreibe einen Kommentar