MySQL Count() question.. (mysql newb)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • knoak

    MySQL Count() question.. (mysql newb)

    Hi there,

    I have a small question:
    I have a table with lots of rows in it. Of course all have a different id,
    but each can be assigned to a certain category. Categories correspond
    with 1 - 10.

    So a row looks like this: 'unique id | 4'

    What i want is to do a count how much rows belong to a certain category.
    Is it possible to do the following (from 1 query)

    echo $counted_rows['4'];

    Which sould return the number of rows with category '4';

    Or is there anything similar if this is wrong?

    Any help is great!! :)
  • Kevin

    #2
    Re: MySQL Count() question.. (mysql newb)

    To get the number of rows with a category '4' use:

    SELECT COUNT( * ) FROM `table` WHERE category = '4';

    To get the row count for all categories in a single query (I think what
    you're asking for):

    SELECT COUNT( category ) AS repetitions, category FROM `table`
    GROUP BY category

    - Kevin

    "knoak" <knoakske@hotma il.com> wrote in message
    news:a5e41e0e.0 502170345.1a9a1 d0c@posting.goo gle.com...[color=blue]
    > Hi there,
    >
    > I have a small question:
    > I have a table with lots of rows in it. Of course all have a different id,
    > but each can be assigned to a certain category. Categories correspond
    > with 1 - 10.
    >
    > So a row looks like this: 'unique id | 4'
    >
    > What i want is to do a count how much rows belong to a certain category.
    > Is it possible to do the following (from 1 query)
    >
    > echo $counted_rows['4'];
    >
    > Which sould return the number of rows with category '4';
    >
    > Or is there anything similar if this is wrong?
    >
    > Any help is great!! :)[/color]


    Comment

    • knoak

      #3
      Re: MySQL Count() question.. (mysql newb)

      Well, to begin: Thank you for helping. :D

      Ehm i know i am not asking for the first, because this would force me
      to run the query for each category.

      Could you please explain a little bit more about the second one? I can't seem
      to get it to work... :( :s

      Thanks again



      "Kevin" <kevin@wxREMOVE 4SPAM3.com> wrote in message news:<L8ydnWTOK vu0DYnfRVn-2Q@comcast.com> ...[color=blue]
      > To get the number of rows with a category '4' use:
      >
      > SELECT COUNT( * ) FROM `table` WHERE category = '4';
      >
      > To get the row count for all categories in a single query (I think what
      > you're asking for):
      >
      > SELECT COUNT( category ) AS repetitions, category FROM `table`
      > GROUP BY category
      >
      > - Kevin
      >
      > "knoak" <knoakske@hotma il.com> wrote in message
      > news:a5e41e0e.0 502170345.1a9a1 d0c@posting.goo gle.com...[color=green]
      > > Hi there,
      > >
      > > I have a small question:
      > > I have a table with lots of rows in it. Of course all have a different id,
      > > but each can be assigned to a certain category. Categories correspond
      > > with 1 - 10.
      > >
      > > So a row looks like this: 'unique id | 4'
      > >
      > > What i want is to do a count how much rows belong to a certain category.
      > > Is it possible to do the following (from 1 query)
      > >
      > > echo $counted_rows['4'];
      > >
      > > Which sould return the number of rows with category '4';
      > >
      > > Or is there anything similar if this is wrong?
      > >
      > > Any help is great!! :)[/color][/color]

      Comment

      • Kevin

        #4
        Re: MySQL Count() question.. (mysql newb)

        What problem are you having? Obviously you need to replace the field and
        table names with the ones from your table.

        "knoak" <knoakske@hotma il.com> wrote in message
        news:a5e41e0e.0 502171037.2efed fe6@posting.goo gle.com...[color=blue]
        > Well, to begin: Thank you for helping. :D
        >
        > Ehm i know i am not asking for the first, because this would force me
        > to run the query for each category.
        >
        > Could you please explain a little bit more about the second one? I can't
        > seem
        > to get it to work... :( :s
        >
        > Thanks again
        >
        >
        >
        > "Kevin" <kevin@wxREMOVE 4SPAM3.com> wrote in message
        > news:<L8ydnWTOK vu0DYnfRVn-2Q@comcast.com> ...[color=green]
        >> To get the number of rows with a category '4' use:
        >>
        >> SELECT COUNT( * ) FROM `table` WHERE category = '4';
        >>
        >> To get the row count for all categories in a single query (I think what
        >> you're asking for):
        >>
        >> SELECT COUNT( category ) AS repetitions, category FROM `table`
        >> GROUP BY category
        >>
        >> - Kevin
        >>
        >> "knoak" <knoakske@hotma il.com> wrote in message
        >> news:a5e41e0e.0 502170345.1a9a1 d0c@posting.goo gle.com...[color=darkred]
        >> > Hi there,
        >> >
        >> > I have a small question:
        >> > I have a table with lots of rows in it. Of course all have a different
        >> > id,
        >> > but each can be assigned to a certain category. Categories correspond
        >> > with 1 - 10.
        >> >
        >> > So a row looks like this: 'unique id | 4'
        >> >
        >> > What i want is to do a count how much rows belong to a certain
        >> > category.
        >> > Is it possible to do the following (from 1 query)
        >> >
        >> > echo $counted_rows['4'];
        >> >
        >> > Which sould return the number of rows with category '4';
        >> >
        >> > Or is there anything similar if this is wrong?
        >> >
        >> > Any help is great!! :)[/color][/color][/color]


        Comment

        • knoak

          #5
          Re: MySQL Count() question.. (mysql newb)

          Yes thanks, that far i got, but how can i call the values in different parts
          of the page, whereever i want.
          So for instance i have 10 tables and in the first one i call $counted_rows_4
          or so, and in the next one $counted_rows_9 . So they correspond with the
          category. It doesnt matter how i call them, as long as i can call them
          individually.

          Hope you get it, and sorry for me being a noob. I googled on this, searched the
          mysql site, but couldn't find anything that makes sense to me.

          Thanks again.




          "Kevin" <kevin@wxREMOVE 4SPAM3.com> wrote in message news:<w4qdnfBkZ JC_Y4nfRVn-2w@comcast.com> ...[color=blue]
          > What problem are you having? Obviously you need to replace the field and
          > table names with the ones from your table.
          >
          > "knoak" <knoakske@hotma il.com> wrote in message
          > news:a5e41e0e.0 502171037.2efed fe6@posting.goo gle.com...[color=green]
          > > Well, to begin: Thank you for helping. :D
          > >
          > > Ehm i know i am not asking for the first, because this would force me
          > > to run the query for each category.
          > >
          > > Could you please explain a little bit more about the second one? I can't
          > > seem
          > > to get it to work... :( :s
          > >
          > > Thanks again
          > >
          > >
          > >
          > > "Kevin" <kevin@wxREMOVE 4SPAM3.com> wrote in message
          > > news:<L8ydnWTOK vu0DYnfRVn-2Q@comcast.com> ...[color=darkred]
          > >> To get the number of rows with a category '4' use:
          > >>
          > >> SELECT COUNT( * ) FROM `table` WHERE category = '4';
          > >>
          > >> To get the row count for all categories in a single query (I think what
          > >> you're asking for):
          > >>
          > >> SELECT COUNT( category ) AS repetitions, category FROM `table`
          > >> GROUP BY category
          > >>
          > >> - Kevin
          > >>
          > >> "knoak" <knoakske@hotma il.com> wrote in message
          > >> news:a5e41e0e.0 502170345.1a9a1 d0c@posting.goo gle.com...
          > >> > Hi there,
          > >> >
          > >> > I have a small question:
          > >> > I have a table with lots of rows in it. Of course all have a different
          > >> > id,
          > >> > but each can be assigned to a certain category. Categories correspond
          > >> > with 1 - 10.
          > >> >
          > >> > So a row looks like this: 'unique id | 4'
          > >> >
          > >> > What i want is to do a count how much rows belong to a certain
          > >> > category.
          > >> > Is it possible to do the following (from 1 query)
          > >> >
          > >> > echo $counted_rows['4'];
          > >> >
          > >> > Which sould return the number of rows with category '4';
          > >> >
          > >> > Or is there anything similar if this is wrong?
          > >> >
          > >> > Any help is great!! :)[/color][/color][/color]

          Comment

          • auron

            #6
            HOW TO GET HOW MANY RECORDS YOU HAVE IN DB

            Hi
            I finally discover the way how to get what I need

            Let's look to my code

            ----------------------- CODE ------------------------

            $res = @mysql_query("S ELECT COUNT(*) AS ToT FROM table_name");
            $dato = mysql_fetch_arr ay($res)

            echo($dato[ToT])
            ----------------------- CODE ------------------------

            With this code you will get how much records do you have in DB

            PS
            Whan you think that everything is over and that you are at bottom o
            trying push harder to see what is under

            Auro
            http://eye.cc -php- web design

            Comment

            • Tony Marston

              #7
              Re: HOW TO GET HOW MANY RECORDS YOU HAVE IN DB

              This gives you the record count for a single TABLE, not the entire DATABASE.
              Learn to use the correct terminology.

              --
              Tony Marston

              This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




              "auron" <mauro@compriov endi-dot-com.no-spam.invalid> wrote in message
              news:kX56e.164$ I85.3628156@new s.sisna.com...[color=blue]
              > Hi,
              > I finally discover the way how to get what I need.
              >
              > Let's look to my code:
              >
              > ----------------------- CODE ------------------------>
              >
              > $res = @mysql_query("S ELECT COUNT(*) AS ToT FROM table_name");
              > $dato = mysql_fetch_arr ay($res);
              >
              > echo($dato[ToT]);
              > ----------------------- CODE ------------------------>
              >
              > With this code you will get how much records do you have in DB.
              >
              > PS:
              > Whan you think that everything is over and that you are at bottom of
              > trying push harder to see what is under.
              >
              > Auron
              > http://eye.cc -php- web design[/color]


              Comment

              Working...