Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Count z kilku tabel
Siner
post
Post #1





Grupa: Zarejestrowani
Postów: 159
Pomógł: 6
Dołączył: 2.01.2004

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


Chcę zliczyć ilość wystąpień rekordów z 3 różnych tabel. Przykład - zliczyć ile użytkownik ma komentarzy, głosów, serwisów. Co mi przyszło do głowy od razu to.
  1. SELECT users.id, COUNT( comments.id ) AS comments_count, COUNT( votes.id ) AS votes_count, COUNT( services.id ) AS service_count
  2. FROM users, comments, votes, services
  3. WHERE users.id =1 AND comments.user_id = users.id AND votes.user_id = users.id AND services.author_id = users.id
  4. GROUP BY users.id

Ale w każdym z tych 3 wierszy z count mam jedną liczbę i to nie koniecznie jakąś konkretną. Macie jakieś sposoby na rozwiązanie togo problemu?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
kefirek
post
Post #2





Grupa: Zarejestrowani
Postów: 781
Pomógł: 256
Dołączył: 29.06.2008

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


Zobacz tak powinno dzialac
  1. SELECT
  2. u.id,
  3. (SELECT COUNT( c.id ) FROM comments c WHERE (u.id=c.user_id)) AS komentarzy,
  4. (SELECT COUNT( v.id ) FROM votes v WHERE (u.id=v.user_id)) AS votes,
  5. (SELECT COUNT( s.id ) FROM services s WHERE (u.id=s.author_id)) AS serwis
  6. FROM users u
  7. WHERE u.id=1


Ten post edytował kefirek 7.03.2009, 12:48:23
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: 30.12.2025 - 05:22