Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: distinct count()
Forum PHP.pl > Forum > Bazy danych > MySQL
jastu
Witam,
jeśli mamy w tabeli trzy wiersze i pole parent_id ma wartości 1, 2, 2 to możemy policzyć ilość unikalnych parent_id tak

  1. SELECT DISTINCT parent_id FROM TABLE


a jak to zrobić z count() ?


Potrzebne mi to do widoku
  1. CREATE VIEW `db_core_`.`f_v_grupy` AS SELECT
  2. g.*,
  3. count(f.id_f) AS ile_for,
  4. count(c.id_k) AS ile_kat,
  5. count(t.id_t) AS ile_tem,
  6. count(r.id_o) AS ile_odp
  7. FROM f_t_groups AS g
  8. LEFT JOIN f_t_forums AS f ON f.id_g = g.id_g
  9. LEFT JOIN f_t_categories AS c ON f.id_f = c.id_f
  10. LEFT JOIN f_t_topics AS t ON c.id_k = t.id_k
  11. LEFT JOIN f_t_replies AS r ON t.id_t = r.id_t
  12. GROUP BY
  13. id_g

obecna wersja zlicza błędnie... sciana.gif
nospor
  1. SELECT count(DISTINCT parent_id) FROM TABLE
jastu
Ręce mi opadły....mogłem sprawdzić w ten sposób.Dzięki !

Szkoda bo :

Cytat
For a view to be updatable, there must be a one to one relatoinship between a view row and a (base) table row. Although I can think of a situation where a join does not preclude this requirement, it seem MySQL chooses to ban joins altogether. At least, the doc:

[dev.mysql.com]

say that a view is not updateble if it contains:

Aggregate functions (SUM(), MIN(), MAX(), COUNT(), and so forth)
DISTINCT
GROUP BY
HAVING
UNION or UNION ALL
Subquery in the select list
Join
Non-updatable view in the FROM clause
A subquery in the WHERE clause that refers to a table in the FROM clause
Refers only to literal values (in this case, there is no underlying table to update)
ALGORITHM = TEMPTABLE (use of a temporary table always makes a view non-updatable)
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.