Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Zliczanie rekordów z 3 tabel
the-sopel
post
Post #1





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

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


Witam, musze zliczyć rekordy z 3 baz, napisałem coś takiego (niestety nie działa, wyświetla 1)
  1. SELECT count(v.id) AS news, count(f.id) AS arts, count(g.id) AS files FROM news v, articles_arts f, download_files g GROUP BY v.id, f.id, g.id

Proszę o pomoc i pozdrawiam.
Go to the top of the page
+Quote Post
DawidF
post
Post #2





Grupa: Zablokowani
Postów: 120
Pomógł: 2
Dołączył: 4.11.2004

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


A czemu próbujesz liczyć w jednym zapytaniu od razu z trzech tabel?

Czy nie lepiej po kolei?


SELECT count(id) AS ile_news FROM news
SELECT count(id) AS ile_articles_arts FROM articles_arts
SELECT count(id) AS ile_download_files FROM download_files


Po drodze przypisać wyniki do zmiennych i tyle...

Tworzyć tylko takie piramidalne zapytania dla... no właśnie: po co?
Go to the top of the page
+Quote Post
Adiasz
post
Post #3





Grupa: Zarejestrowani
Postów: 155
Pomógł: 0
Dołączył: 28.02.2004

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


  1. SELECT count(id) FROM news UNION
  2. SELECT count(id) FROM articles_arts UNION
  3. SELECT count(id) FROM download_files

albo:

  1. SELECT count(id),(SELECT count(id) FROM articles_arts),(SELECT count(id) FROM download_files) FROM news
Go to the top of the page
+Quote Post
the-sopel
post
Post #4





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

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


Próbowałem zastosować sposób Adiasza.
Napisałem to tak:
  1. SELECT count(id) AS news,(SELECT count(id) AS arts FROM articles_arts),(SELECT count(id) AS files FROM download_files) FROM news

I wyświetliło
SQL Error : 1064 You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT count(id) as arts FROM articles_arts),(SELECT count(id)
Dalszy kod wygląda tak:
  1. <?php
  2. if( !($result = $db->sql_query($sql)) )
  3. {
  4. message_die(GENERAL_ERROR, "I couldn't select", '', __LINE__, __FILE__, $sql);
  5. }
  6.  
  7. $row = $db->sql_fetchrow($result);
  8. $template->assign_vars(array(
  9. 'FILES' => $row['files'],
  10. 'NEWS' => $row['news'],
  11. 'ARTS' => $row['arts'])
  12. );
  13. ?>
Go to the top of the page
+Quote Post
kossa
post
Post #5





Grupa: Zarejestrowani
Postów: 1 165
Pomógł: 9
Dołączył: 9.04.2002
Skąd: Toruń

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


Sprawdź wersję swojej bazy danych i sprawdz czy posiada ona możliwość tworzenia podazapytań.

subquery

Łukasz
Go to the top of the page
+Quote Post
the-sopel
post
Post #6





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

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


Hmm, no jak nie działa to znaczy, że nie ma.
Błąd chyba też o tym mówi.
Dlatego chciałem zrobić to jednym zapytaniem (pierwszy post).
Wie ktoś jak zrobić by to działało?

|EDIT|
Problem już rozwiązany, dzięki za pomoc.

Ten post edytował the-sopel 18.08.2006, 19:05:46
Go to the top of the page
+Quote Post

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: 14.09.2025 - 23:42