Delphi-PRAXiS
Seite 2 von 3     12 3      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Datenbanken (https://www.delphipraxis.net/15-datenbanken/)
-   -   [MYSQL] Letzte Einträge pro kategorie (https://www.delphipraxis.net/94386-%5Bmysql%5D-letzte-eintraege-pro-kategorie.html)

Jelly 20. Jun 2007 21:37

Re: [MYSQL] Letzte Einträge pro kategorie
 
Brrrr.... Ich Depp hab ja auch einfach das group by verduselt:
SQL-Code:
SELECT cat_id, max (image_date) as last_image_date
FROM 4images_images
group by cat_id
gibt dann Folgendes aus:
Code:
1 2 
2 12 
5 6 
6 20

DeddyH 20. Jun 2007 21:41

Re: [MYSQL] Letzte Einträge pro kategorie
 
Siehste, das ist Schritt 1. Wenn dann noch gejoint werden muss, wird es etwas kniffliger. Aber hast schon Recht, so isses IMHO richtig.

Jelly 20. Jun 2007 21:43

Re: [MYSQL] Letzte Einträge pro kategorie
 
Ich sag ja... Habs nur verschwitzt :wall:

DeddyH 20. Jun 2007 21:45

Re: [MYSQL] Letzte Einträge pro kategorie
 
Schön, dass wir uns schlussendlich doch einig geworden sind, oder? :thumb:

Jelly 20. Jun 2007 21:47

Re: [MYSQL] Letzte Einträge pro kategorie
 
Darauf mach ich mir doch glatt mal nen Wein auf. :wink:

_frank_ 21. Jun 2007 00:15

Re: [MYSQL] Letzte Einträge pro kategorie
 
erstmal danke für eure Antworten...
der Link von DeddyH hat mir schon geholfen (auch wenns Arg kompliziert in meinen Augen ist...)
jellys link gibt mir leider nur die benannten spalten an, und lässt kein * zu...

meine aktuelle Abfrage sieht so aus:
SQL-Code:
SELECT * FROM 4images_images i2 WHERE i2.image_date = (SELECT MAX(i1.image_date) AS neustes FROM 4images_images i1 WHERE i1.cat_id = i2.cat_id);
jetzt hatte ich probiert mir den Kategoriename aus einer anderen Tabelle zu holen. normal geht das ja so:

SQL-Code:
SELECT * , 4images_categories.cat_name AS catname FROM 4images_images,4images_categories WHERE 4images_categories.cat_id = 4images_images.cat_id
meine bisherigen versuche, dies in mein aktuelles set einzubauen werden, bringt mir mehr resultsets als ursprünglich, statt nur den name oder einen Mysql-Fehler durch die variablenzuweisung (i2). mal einer der vielen versuche:

SQL-Code:
SELECT * FROM , 4images_categories.cat_name AS catname
FROM 4images_images i2 ,4images_categories
WHERE (4images_categories.cat_id = 4images_images.cat_id) and
  (i2.image_date =
    (SELECT MAX(i1.image_date) AS neustes FROM 4images_images i1 WHERE i1.cat_id = i2.cat_id)
  );
Gruß Frank

DeddyH 21. Jun 2007 07:13

Re: [MYSQL] Letzte Einträge pro kategorie
 
Mal schnell heruntergetippt:
SQL-Code:
SELECT A.*,B.cat_name AS catname
FROM 4images_images A
JOIN 4images_categories B ON B.cat_id = A.cat_id
WHERE A.image_date = (
    SELECT MAX(C.image_date)
    FROM 4images_images C
    WHERE C.cat_id = A.cat_id)
So sollte es richtig sein. Wenn nicht, bitte nochmal melden.

_frank_ 21. Jun 2007 11:32

Re: [MYSQL] Letzte Einträge pro kategorie
 
danke dir deddyH,
funktioniert wie es soll...jetzt mus ich mich da nur reindenken ;), aber dazu gibts ja genug doku...

Gruß Frank

DeddyH 21. Jun 2007 12:17

Re: [MYSQL] Letzte Einträge pro kategorie
 
Vielleicht ist es besser zu verstehen, wenn man es etwas umformuliert:
SQL-Code:
SELECT A.*,B.cat_name AS catname
FROM 4images_images A, 4images_categories B
WHERE B.cat_id = A.cat_id
AND A.image_date = (
    SELECT MAX(C.image_date)
    FROM 4images_images C
    WHERE C.cat_id = A.cat_id)

Jelly 21. Jun 2007 12:22

Re: [MYSQL] Letzte Einträge pro kategorie
 
Was spricht gegen:
SQL-Code:
select
  B.cat_id, B.cat_name, max(A.image_date) as Max_image_date
from
  4images_images A
inner join
  4images_categories B on B.cat_id = A.cat_id
group by
  B.cat_id, B.cat_name
Warum also mit SubSelects arbeiten, was die Abfrage bestimmt nicht schneller macht.


Alle Zeitangaben in WEZ +1. Es ist jetzt 23:17 Uhr.
Seite 2 von 3     12 3      

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-2025 by Thomas Breitkreuz