SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    952
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Counting Unique Records...

    I have the following query that counts the number of active records in my database:

    $query = "SELECT count(*) as artcnt from tbl_articles where active='Y'";


    Now I need to perform another query on the same database that will return the number of unique authors (each author could write more than one article, hence multiple records in the db). I want to count the number of unique authors regardless of whether or not the article is active.

    I tried this:

    $query2 = "SELECT count(*) as authcnt from tbl_articles GROUP BY author";

    but it doesn't seem to work. does anyone have any suggestions?

    brian.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think I would store all the authors in a seperate table with a unique ID for each. Then instead of author name in the article table, use the ID. This makes it easy to store and update information about each author. And of course it makes it faster and easier to count the number of authors.

  3. #3
    googlicious graymatter bvarvel's Avatar
    Join Date
    Sep 2002
    Location
    Katy, TX
    Posts
    952
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    don't need that much functionality for this particular site, otherwise I would. I ended up using mysql_numrows to get around the problem. thanks.

  4. #4
    SitePoint Addict devil cat's Avatar
    Join Date
    Apr 2003
    Location
    Reno
    Posts
    344
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    silan's idea is the best way to go, but the function array_count_values() will tell you how many times each value (in your case author) occurs in the array.

    array_count_values($array);


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •