0

assume I have a table that contain a column named post_id and it has the result like

1
1
1
2
2
3
1
1

I want to loop through all the records and count how many times they exist. What I could thought of is

by while loop

if(result[] = 1){$1++}, but the problem is the value of record is not fixed, it can be 9999..

I'd tried

while ($something= $item->fetch_array()) {
  while($test[] = $something['post_id'] > 0){
//logic here
 }
}
user3346088
  • 109
  • 1
  • 7
  • possible duplicate of [mysql count duplicates](http://stackoverflow.com/questions/3935078/mysql-count-duplicates) –  Mar 03 '14 at 01:42

3 Answers3

1
select post_id, count(*)
from table
group by post_id
Turophile
  • 3,367
  • 1
  • 13
  • 21
1

This is something you can do in SQL. I believe it would be the following:

SELECT post_id, COUNT(*) FROM tablename GROUP BY post_id;

This will return, for each post_id in the table, that post_id and the count of rows with that post_id.

OSborn
  • 895
  • 4
  • 10
  • I don't understand your question. You will get two columns from that query, the first is the post_id, the second is the count of rows with that ID. You can use fetch_array and then access $arr[0] or $arr[1]. – OSborn Mar 03 '14 at 01:48
0

Have you try this. Assume:

Table one

**Table one** 
Column1
1
1 
1 
2 
2 
3
1 
1

You can use this query to count it.

  SELECT one.column1, COUNT(two.column1) FROM one as one, one as two;