Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [MySQL][PHP]Kilka warunków i zapytań do jednej tabeli
emil1702
post 11.02.2013, 11:20:31
Post #1





Grupa: Zarejestrowani
Postów: 23
Pomógł: 0
Dołączył: 10.01.2012

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


Witam,

chcę zrobić zapytanie do bazy, które z tabeli riderpoints będzie pobierać najlepszego zawodnika kolejki, jak optymalnie wykonać to zapytanie?

struktura tabeli
  1. ID | IDRIDER | GAMEWEEK | STARTS | POINTS
  2. 1 | 1 | 1 | 2 | 3
  3. 2 | 2 | 1 | 5 | 4 < - - pobiera TO
  4. 3 | 3 | 2 | 3 | 1 < - - i TO


Próbowałem tak jak poniżej, ale może pobrać tylko jeden wynik, np. id a potrzebuje wszystko

  1. SELECT (
  2.  
  3. SELECT id
  4. FROM riderpoints
  5. WHERE gameweek =1
  6. ORDER BY points DESC
  7. LIMIT 1
  8. ) as col1 ,(
  9.  
  10. SELECT id
  11. FROM riderpoints
  12. WHERE gameweek =2
  13. ORDER BY points DESC
  14. LIMIT 1
  15. ) as col2 , (
  16.  
  17.  
  18. SELECT id
  19. FROM riderpoints
  20. WHERE gameweek =3
  21. ORDER BY points DESC
  22. LIMIT 1
  23. )
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 2)
mmmmmmm
post 11.02.2013, 12:06:56
Post #2





Grupa: Zarejestrowani
Postów: 1 421
Pomógł: 310
Dołączył: 18.04.2012

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


SELECT r.* FROM riverpoints r JOIN (SELECT gameweek, Max(points) points FROM riverpoints GROUP BY gameweek) x ON r.gameweek=x.gameweek AND r.points=x.points
Go to the top of the page
+Quote Post
emil1702
post 15.02.2013, 19:14:10
Post #3





Grupa: Zarejestrowani
Postów: 23
Pomógł: 0
Dołączył: 10.01.2012

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


extra, dzięki wielkie smile.gif)

A jeszcze mam pytanie jak pobrać optymalnie imię i nazwisko z tabeli riders? Jak można zmodyfikować to zapytanie?:

  1. SELECT rp.id FROM riderpoints rp
  2. JOIN (SELECT gameweek, Max(points) points FROM riderpoints GROUP BY gameweek) x
  3. ON rp.gameweek=x.gameweek AND rp.points=x.points
  4. ORDER BY x.gameweek


Tak moze być?
  1. SELECT rp.id, r.name, r.surname, r.club, r.ksm, c.name AS 'club_name', c.city FROM riderpoints rp
  2. JOIN riders r ON r.id = rp.id
  3. JOIN club c ON c.id = r.club
  4. JOIN (SELECT gameweek, Max(points) points FROM riderpoints GROUP BY gameweek) x
  5. ON rp.gameweek=x.gameweek AND rp.points=x.points
  6. ORDER BY x.gameweek


Ten post edytował emil1702 16.02.2013, 14:16:23
Go to the top of the page
+Quote Post

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

 



RSS Wersja Lo-Fi Aktualny czas: 16.07.2025 - 14:46