Autor Beitrag
Aya
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1964
Erhaltene Danke: 15

MacOSX 10.6.7
Xcode / C++
BeitragVerfasst: Fr 01.08.14 15:15 
Hi,

ich habe eine MySQL Tabelle mit folgenden Spalten: "key, regionID, timestamp".
In der Tabelle werden zu bestimmten Ereignissen der timestamp (wann es passiert ist), sowie die regionID (wo es passiert ist) gespeichert.

Es gibt insgesamt 50 verschiedene Regionen und ich würde gerne den neuesten timestamp zu jeder Region wissen.
Gibt es da irgendeine Möglichkeit das in einem Query zu machen?

Also nicht indem ich 50x diesen Query mache:
ausblenden SQL-Anweisung
1:
SELECT `timestampFROM `events` WHERE `regionID` = ? LIMIT 0,1					


sondern alles in einem mit 50 Zeilen als result - irgendwie machbar?


Danke,
Daniela

_________________
Aya
I aim for my endless dreams and I know they will come true!
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: Fr 01.08.14 15:50 
Versuch mal
ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
6:
7:
SELECT 
  `regionID`,
  MAX(`timestamp`)
FROM
  `events`
GROUP BY
  `regionID`
Aya Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1964
Erhaltene Danke: 15

MacOSX 10.6.7
Xcode / C++
BeitragVerfasst: Fr 01.08.14 16:22 
Funktioniert, cool! Vielen dank :D

_________________
Aya
I aim for my endless dreams and I know they will come true!
Aya Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1964
Erhaltene Danke: 15

MacOSX 10.6.7
Xcode / C++
BeitragVerfasst: Fr 15.08.14 23:22 
Leider ist jetzt doch noch ein Problem aufgetreten... jede Zeile in der Tabelle hat auch einen "key" der unique und auto_increment ist.

Ich würde jetzt gern zusätzlich den Key zu dem MAX timestamp bekommen.. geht das irgendwie, ohne das ich das tiemstamp-field UNIQUE mache und dann per JOIN den Key hole?

_________________
Aya
I aim for my endless dreams and I know they will come true!
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19272
Erhaltene Danke: 1740

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: Sa 16.08.14 08:02 
Meinst du vielleicht das?
sqlfiddle.com/#!2/54ab9/2
ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
select e1.regionID, e1.timestamp, e1.key from events e1
left outer join events e2
on e1.regionID = e2.regionID and e1.timestamp < e2.timestamp
where e2.regionID is null
order by e1.regionID;
WasWeißDennIch
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 653
Erhaltene Danke: 160



BeitragVerfasst: Sa 16.08.14 14:11 
Oder auch
ausblenden SQL-Anweisung
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
SELECT
  E.`regionID`,
  E.`timestamp`,
  E.`key`
FROM
  `events` E
WHERE
  `timestamp` = 
    (SELECT 
       MAX(`timestamp`)
     FROM
       `events`
     WHERE
       `regionID` = E.`regionID`)
ORDER BY
  E.`regionID`