Jump to content

Couting how many records have the same id.


son.of.the.morning

Recommended Posts

A tables id is usually its primary key and should be unique across that entire table. Some tables may contain an id as a reference back to a "foreign" table, these are foreign keys. So in your case you would have something kinda of like this:

 

table: groups
-----------------
id | group
-----------------
0  | root
1  | wheel
2  | nobody

table: users
----------------
id | username | group_id
------------------------
0  | root     | 0
1  | admin    | 1
2  | michael  | 1
3  | gob      | 1
4  | buster   | 2

 

So as you can see in users table, group_id can be repeated (and thats fine). To query the DB and return username and groupname of the user, you would do:

SELECT group.groups, username.users FROM groups, users WHERE group_id.users = id.groups

Link to comment
Share on other sites

this is helpful fo course but it's not what am trying to do. I dont want to show the relation between two tables even though they are. All i want to do if to be able to count how many records have the same content.

 

for example if you have category_id which can have more then one record with the same id in. I want to be able to say how many records in the table have the same category id. Hope this makes sence

Link to comment
Share on other sites

SELECT COUNT(*) FROM yourTable WHERE category_id = 4;

Unless of course you mean something else, the sentence you said has four possible interpretations, I chose the easiest one because I'm not done with my coffee yet.  Show data, and show the kind of result you're looking for.

Link to comment
Share on other sites

We can't help you because we're guessing about what you want based on your complete lack of detail and poorly explained problem.  So, again:

 

SHOW US YOUR TABLE(S)

 

And tell us explicitly how you want to use the data from which column(s).

Link to comment
Share on other sites

Okay, now we're getting somewhere.

 

You need to fetch the results after you run the query, just like normal:

 

$query = "SELECT COUNT(*) FROM table WHERE category_id = $catID";
$result = mysql_query($query);
$row = mysql_fetch_array($result);

echo "Number of entries: " . $row[0];

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.