Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Wybieranie wierszy, ktorych jedno z pol ..., powtarza sie najczesciej.
Draugfor
post
Post #1





Grupa: Zarejestrowani
Postów: 131
Pomógł: 0
Dołączył: 13.07.2003
Skąd: Torun/Poznan

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


Hej!
Wiem, temat nic nie mowi, wiec przejde od razu do rzeczy.

Mam tabele albums:

Kod
mysql> select * from albums;
+----+--------------------+---------------+
| ID | title              | band          |
+----+--------------------+---------------+
|  1 | Load               | Metallica     |
|  2 | Master of Puppets  | Metallica     |
|  3 | Made In Heaven     | Queen         |
|  4 | Melissa            | Mercyful Fate |
|  5 | Chamber Music      | Coal Chamber  |
|  6 | A Day At The Races | Queen         |
+----+--------------------+---------------+


I chce, zeby zapytanie zwrocilo te albumy i nazwe tego zespolu (zespolow), ktory ma tych albumow najwiecej, czyli tutaj Queen i Metallica.

Innymi slowy oczekuje wyniku:
Kod
Load               | Metallica     |
Master of Puppets  | Metallica     |
Made In Heaven     | Queen         |
A Day At The Races | Queen         |


Wykombinowalem tylko takie zapytanie:
  1. mysql> SELECT band, count(*) AS ilosc
  2. FROM albums GROUP BY band HAVING ilosc=(SELECT count(*) AS i1
  3. FROM albums GROUP BY band ORDER BY i1 DESC LIMIT 1 ) ORDER BY ilosc DESC;

ktore zwraca:
Kod
+-----------+-------+
| band      | ilosc |
+-----------+-------+
| Metallica |     2 |
| Queen     |     2 |
+-----------+-------+


Czy ktos wie, jak to zrobic? smile.gif
Pozdrawiam!


--------------------
On the day I was born, the nurses all gathered 'round
And they gazed in wide wonder, at the joy they had found
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 4)
mario
post
Post #2





Grupa: Zarejestrowani
Postów: 186
Pomógł: 0
Dołączył: 23.09.2003
Skąd: Siemianowice Śląskie

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


To już było poruszane Jak sprawdzić największą wartość?
  1. SELECT MAX(nazwaKolumny)
  2. FROM nazwaTabeli;

zanim napiszesz posta użyj wyszukiwarki


--------------------
W życiu piękne są tyko chwile....
Życie choć piękne tak krótkie jest....
Wystarczy chwila by zgasić je....
Go to the top of the page
+Quote Post
Draugfor
post
Post #3





Grupa: Zarejestrowani
Postów: 131
Pomógł: 0
Dołączył: 13.07.2003
Skąd: Torun/Poznan

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


Madrala - a Ty przeczytaj pytanie zanim napiszesz odpowiedz.
Co mam niby z tym maxem zrobic? Heh.



EDIT

No dobra, sam cos wymyslilem. Wyglada obrzydliwie; znajdzie sie smialek, ktory to zoptymalizuje?

  1. SELECT band, title
  2. FROM albums WHERE concat(band,(SELECT count(*) AS c1
  3. FROM albums GROUP BY band ORDER BY c1 DESC LIMIT 1 ))=any(SELECT concat(band, count(*)) AS ilosc
  4. FROM albums GROUP BY band HAVING count(*)=(SELECT count(*) AS c2
  5. FROM albums GROUP BY band ORDER BY c2 DESC LIMIT 1 ));


Ten post edytował Draugfor 7.05.2005, 21:02:58


--------------------
On the day I was born, the nurses all gathered 'round
And they gazed in wide wonder, at the joy they had found
Go to the top of the page
+Quote Post
behemot
post
Post #4





Grupa: Zarejestrowani
Postów: 45
Pomógł: 0
Dołączył: 17.07.2003
Skąd: Tarnów/Kraków

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


Witam,

Ja wykombinowalem cos takiego :)
Ale to prawie to samo co Ty
  1. SELECT title,band
  2. FROM albums WHERE band IN (SELECT band
  3. FROM albums GROUP BY band HAVING count(*) >= (SELECT count(*) AS ilosc
  4. FROM albums GROUP BY band ORDER BY ilosc DESC LIMIT 1 )-1);

Testowalem na postgresie ale w mysql tez chyba powinno dzialac :)

Pozdrawiam,
kuba
Go to the top of the page
+Quote Post
Draugfor
post
Post #5





Grupa: Zarejestrowani
Postów: 131
Pomógł: 0
Dołączył: 13.07.2003
Skąd: Torun/Poznan

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


No nie pogadasz! cool.gif
Wlasnie o to chodzilo! Tylko to -1 w ostatniej linijce jest niepotrzebne, a tak to gra i buczy, wielkie dzieki!
Kamien z serca. biggrin.gif

Pozdrawiam Behemot!


--------------------
On the day I was born, the nurses all gathered 'round
And they gazed in wide wonder, at the joy they had found
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 Aktualny czas: 19.08.2025 - 17:59