Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [mysql] wyciagniecie ostatniego rekordu wraz z GROUP BY
AndyPSV2
post
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 3.11.2011

Ostrzeżenie: (10%)
X----


zapytanie jakie mam:
  1. SELECT *,COUNT(*) AS cnt FROM '.PRFX.'messages WHERE _to = "'.$this->u['id'].'" GROUP BY _from ORDER BY id DESC LIMIT '.($pg*$PER_PG).','.$PER_PG.'')


  1. id _from _to date message read
  2. 1 3 1 2011-11-07 13:49:54 dasdsasaddsadsa 0
  3. 2 3 1 2011-11-07 13:50:18 newest message 0


wynik jaki dostaje:
  1. array ( 0 => array ( 'id' => '1', '_from' => '3', '_to' => '1', 'date' => '2011-11-07 13:49:54', 'message' => 'dasdsasaddsadsa', 'read' => '0', 'cnt' => '2', ), )


wynik jaki chce uzyskac:
  1. array ( 0 => array ( 'id' => '2', '_from' => '3', '_to' => '1', 'date' => '2011-11-07 13:50:18', 'message' => 'newest message', 'read' => '0', 'cnt' => '2', ), )


jak to zrobic?

kombinowalem roznie, ale nie wyszlo np.
  1. 'SELECT id,date,message,COUNT(*) as cnt FROM (SELECT MAX(date) AS date,message FROM '.PRFX.'messages WHERE _to = "'.$this->u['id'].'" GROUP BY _from ORDER BY date DESC) LEFT JOIN '.PRFX.'messages USING (date,message) LIMIT '.($pg*$PER_PG).','.$PER_PG.''
Go to the top of the page
+Quote Post
Sephirus
post
Post #2





Grupa: Zarejestrowani
Postów: 1 527
Pomógł: 438
Dołączył: 28.06.2011
Skąd: Warszawa

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


  1. SELECT *,COUNT(*) AS cnt FROM (SELECT * FROM '.PRFX.'messages WHERE _to = "'.$this->u['id'].'" ORDER BY id DESC) x GROUP BY _fromR ORDER BY id DESC LIMIT '.($pg*$PER_PG).','.$PER_PG.'')
  2.  


Ten post edytował Sephirus 7.11.2011, 15:52:05


--------------------
If you're good at something, never do it for free.
Potrzebujesz skryptu JS lub PHP - szukasz kogoś kto przetestuje twoją aplikację pod względem bezpieczeństwa? Szybko i solidnie? Napisz ;)
Mój blog - Jak zwiększyć wydajność front-endu - O buforowaniu wyjścia w PHP słów kilka...
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: 21.08.2025 - 19:02