Autor Beitrag
FinnO
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1331
Erhaltene Danke: 123

Mac OSX, Arch
TypeScript (Webstorm), Kotlin, Clojure (IDEA), Golang (VSCode)
BeitragVerfasst: Mo 26.09.11 18:20 
Moin,

ich bastel gerade an einer Datenbank. Dabei werden einem Produkt mehrere Preise an verschiedenen Daten zugeordnet. Das ganze schaut dabei ungefähr so aus:

Tabelle: articles
ausblenden Quelltext
1:
2:
3:
4:
id - title
1  - Obst
2  - Gemüse
3  - Bratwurst


Tabelle: prices
ausblenden Quelltext
1:
2:
3:
4:
id - article_id - date        - price
1  - 1          - gestern     - 27.99
2  - 1          - heute       - 28.99
3  - 2          - heute       - 12.99


Ich möchte jetzt bei einer kompletten Auflistung aller Artikel zu jedem Artikel nur den aktuellsten Preis anzeigen.

Also in diesem Fall zum Artikel 1 nur den Preis 28.99 ausgeben, nicht den Preis 27.99.

Ist das möglich? Und wenn ja, wie?
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Mo 26.09.11 18:43 
ausblenden SQL-Anweisung
1:
SELECT a.*, p.price FROM articles AS a INNER JOIN prices AS p ON a.id = p.article_id, prices AS p2 WHERE p.id = MAX(p2.id) GROUP BY p2.article_id					


Wenn ich mich grad nicht vertan hab.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
FinnO Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1331
Erhaltene Danke: 123

Mac OSX, Arch
TypeScript (Webstorm), Kotlin, Clojure (IDEA), Golang (VSCode)
BeitragVerfasst: Mo 26.09.11 18:48 
Moin,

PMA markiert MAX rot und behauptet invalid Use of Group by...

Hmm..
FinnO Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1331
Erhaltene Danke: 123

Mac OSX, Arch
TypeScript (Webstorm), Kotlin, Clojure (IDEA), Golang (VSCode)
BeitragVerfasst: Mo 26.09.11 20:16 
Moin,

Momentan bin ich so weit, dass ich schonmal das neuere der Daten angezeigt bekomme, nur nicht den zum Datensatz passenden Preis:

ausblenden SQL-Anweisung
1:
SELECT MAX(p.`date`) AS date, p.`price` FROM `prices`  AS p GROUP BY `article_id`					


liefert zwar als Datum "heute" aber noch als Preis 27.99 :(
FinnO Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1331
Erhaltene Danke: 123

Mac OSX, Arch
TypeScript (Webstorm), Kotlin, Clojure (IDEA), Golang (VSCode)
BeitragVerfasst: Mo 26.09.11 20:33 
Moin,

zur Krönung nun ein Dreifachpost - Dank Bennys Hilfe habe ich fertig:

ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
6:
7:
8:
SELECT p.price, p.date
FROM prices AS p
WHERE p.id
IN (
SELECT MAX( p.id )
FROM prices AS p
GROUP BY p.article_id
)