Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Ostatnie zdarzenie dla określonego warunku
scanner
post
Post #1





Grupa: Zarząd
Postów: 3 503
Pomógł: 28
Dołączył: 17.10.2002
Skąd: Wrocław




Mam dwie tabele stworzone według zapytań jak niżej.
W "events" zapisuję pewne zdarzenia powtarzalne w czasie wykonywane przez osoby z tabelki "persons". Każde zdarzenie ma jak widać dwa stany. Moje pytanie jest nastepujące:

Jak wydostać sName wszystkich osób, których ostatnim wykonanym zdarzeniem jest 'In'?

Próbowąłem pokombinowąć trochę, ale jedyne co mi wysżło to:
  1. SELECT persons.sCode, persons.sName, events.iTimestamp
  2. FROM events LEFT JOIN persons ON events.sCode = persons.sCode
  3. WHERE sAction = 'In'
  4. GROUP BY events.sCode
  5. ORDER BY events.iTimestamp DESC

  1. -- Wersja serwera: 4.0.22
  2. -- Wersja php: 5.0.2
  3.  
  4. CREATE TABLE `events` (
  5. `iID` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  6. `sCode` varchar(12) NOT NULL DEFAULT '0',
  7. `iTimestamp` int(11) NOT NULL DEFAULT '0',
  8. `sAction` SET('In','Out') DEFAULT NULL,
  9. PRIMARY KEY (`iID`)
  10. ) TYPE=MyISAM;
  11.  
  12. CREATE TABLE `persons` (
  13. `iID` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  14. `sCode` varchar(12) NOT NULL DEFAULT '0',
  15. `sName` varchar(255) NOT NULL DEFAULT '',
  16. `iBirthdate` int(11) NOT NULL DEFAULT '0',
  17. `sAppointmentCode` varchar(12) NOT NULL DEFAULT '0',
  18. `sAppointmentName` varchar(255) NOT NULL DEFAULT '',
  19. `sPhoto` longblob NOT NULL,
  20. PRIMARY KEY (`iID`)
  21. ) TYPE=MyISAM;
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
popbart
post
Post #2





Grupa: Zarejestrowani
Postów: 255
Pomógł: 0
Dołączył: 22.04.2004
Skąd: Żoliborz

Ostrzeżenie: (0%)
-----


1.jeżeli jeden persons może mieć wiele events to dlaczego nie ma poł±czenia relacyjnego(w tabeli events pole id_persont(int)) tylko przez pola varchar?
2.jak z takiego varchar wyciagn±c max() (ostatnie zdarzenie)?

jeżeli przyjmiemy że przerobisz tabele i bedziesz miał mysql'a >4.1.x to:

  1. SELECT persons.iID, persons.sName, events.iTimestamp
  2. FROM persons LEFT JOIN events ON persons.iID = events.iID_persons
  3. WHERE evets.iID IN(SELECT max(iID) FROM events GROUP BY iID_persons) AND events.sAction = 'In'
  4. ORDER BY events.iTimestamp DESC


  1. persons.iID, persons.sName
  2. FROM persons LEFT JOIN events ON persons.iID = events.iID_persons
  3. WHERE events.iID_persons IS NULL


Ten post edytował popbart 20.12.2004, 17:38:22
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 28.12.2025 - 15:22