473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

MySql PHP, count and group by question pretty tricky

Hey Guys,

Ok here is my problem:

There is around 350 rows in the db.

All the variables $actor_id, $comment_id and $likes_id are all uid's for facebook users

Now i want to group and sum how many entries there are of each unique uid

Say for example the number:

'65653171' came up
10 times as a $actor_id
5 times as a $comment_id
3 times as a $likes_id

and

'67418530' came up
5 times as a $actor_id
21 times as a $comment_id
3 times as a $likes_id

Then i want a query that would display in order of most entries:
1.) 67418530 (29)
2.) 65653171 (18)


Thanks for the help, real brain teazer for me.


This is the code i use to fetch the Data from the FQL Table, a preview of the FQL Table can been seen on the bottom of this thread.

Expand|Select|Wrap|Line Numbers
  1.  $query = "SELECT post_id, actor_id, comments, likes FROM stream WHERE source_id = ".$user." LIMIT 500";
  2. $result = mysql_query($query);
  3. $count = count($result);
  4. $total=$count;
  5.  
  6.  
  7.  for ($i = 0; $i < $count; $i++)
  8.  {
  9.  
  10.  $actor_id = $result[$i][actor_id];
  11. $count_c  = $result[$i][comments][count];
  12.  
  13. if($count_c > 0){
  14.     for ($a = 0; $a < $count_c; $a++){
  15.           $comment_id = $result[$i][comments][comment_list][$a][fromid];
  16.  
  17.     }
  18. }
  19.  
  20.  
  21. $count_l  = $result[$i][likes][count];
  22.  
  23. if($count_l > 0){        
  24.         for ($b = 0; $b < $count_l; $b++){
  25.             $likes_id = $result[$i][likes][friends][$b];
  26.  
  27.    }        
  28. }
  29.  
  30.  
  31.  
  32.  
  33. }
This is how the information is stored in the DB

Expand|Select|Wrap|Line Numbers
  1. [posts] => Array
  2.  
  3.         (
  4.  
  5.             [0] => Array
  6.  
  7.                 (
  8.  
  9.                     [post_id] => 65653171_199696556137
  10.  
  11.                     [viewer_id] => 65653171
  12.  
  13.                     [source_id] => 65653171
  14.  
  15.                     [type] => 46
  16.  
  17.                     [app_id] => 291512034
  18.  
  19.                     [attribution] => via <a href="/mobile/?v=web">Mobile Web</a>
  20.  
  21.                     [actor_id] => 65653171
  22.  
  23.                     [target_id] => 
  24.  
  25.                     [message] => is just trying to be a nice guy :P stop hating on me.
  26.  
  27.                     [attachment] => 
  28.  
  29.                     [app_data] => 
  30.  
  31.                     [action_links] => 
  32.  
  33.                     [comments] => Array
  34.  
  35.                         (
  36.  
  37.                             [can_remove] => 1
  38.  
  39.                             [can_post] => 1
  40.  
  41.                             [count] => 2
  42.  
  43.                             [comment_list] => Array
  44.  
  45.                                 (
  46.  
  47.                                     [0] => Array
  48.  
  49.                                         (
  50.  
  51.                                             [fromid] => 67418530
  52.  
  53.                                             [time] => 125705944
  54.  
  55.                                             [text] => Y these ppl hating on yu bebe :) miss yu sooo much!!
  56.  
  57.                                             [id] => 65653171_199696556137_6751049
  58.  
  59.                                         )
  60.  
  61.  
  62.  
  63.                                     [1] => Array
  64.  
  65.                                         (
  66.  
  67.                                             [fromid] => 108832181
  68.  
  69.                                             [time] => 1257186632
  70.  
  71.                                             [text] => yo
  72.  
  73.                                             [id] => 65653171_199696556137_6802381
  74.  
  75.                                         )
  76.  
  77.  
  78.  
  79.                                 )
  80.  
  81.  
  82.  
  83.                         )
  84.  
  85.  
  86.  
  87.                     [likes] => Array
  88.  
  89.                         (
  90.  
  91.                             [href] => http://www.facebook.com/social_graph.php?node_id=19966556137&class=LikeManager
  92.  
  93.                             [count] => 4
  94.  
  95.                             [sample] => 
  96.  
  97.                             [friends] => Array
  98.  
  99.                                 (
  100.  
  101.                                     [0] => 58579401
  102.  
  103.                                     [1] => 55125266
  104.  
  105.                                     [2] => 57449386
  106.  
  107.                                     [3] => 60906602
  108.  
  109.                                 )
  110.  
  111.  
  112.  
  113.                             [user_likes] => 0
  114.  
  115.                             [can_like] => 1
  116.  
  117.                         )
  118.  
  119.  
  120.  
  121.                     [privacy] => Array
  122.  
  123.                         (
  124.  
  125.                             [value] => NOT_EVERYONE
  126.  
  127.                         )
  128.  
  129.  
  130.  
  131.                     [updated_time] => 1257186632
  132.  
  133.                     [created_time] => 1257057706
  134.  
  135.                     [tagged_ids] => 
  136.  
  137.                     [is_hidden] => 0
  138.  
  139.                     [filter_key] => 
  140.  
  141.                     [permalink] => http://www.facebook.com/profile.php?id=65653171&v=feed&story_fbid=19969655137
  142.  
  143.                 )
  144.  
  145.  
  146.  
  147.             [1] => Array
  148.  
  149.                 ( .........Next entry same format as top........
Nov 3 '09 #1
1 3186
TheServant
1,168 Expert 1GB
I am assuming that it will grow beyond 350 rows.

Besides making a rediculous query with lots of filtering you can't do this in one step. It would be way too resource intensive even if you could if it greew to say 10,000 rows.
I suggest this:
Have another column called entry_totalizer or something and then you can ORDER BY that and display that instead of doing a whole lot of COUNTing. Every time a comment_id or actor_id etc is entered, a filter can add one to the entry_totalizer.

With the ones that you already have, you might need to write a simple script to update those already in the table by searching for the empty ones and then doing the math and updating the value for each. If you are worried about resources, LIMIT it to 50 at a time, but I suspect that only having 350 will not be a problem.
Nov 3 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: James | last post by:
HI, I'm looking for a script that will allow users/admins to have a one click backup solution for a MYSQL Database.. 'BACK DATABASE' button, click and its done... The a restore option, that...
0
by: Philip Stoev | last post by:
Hi all, Please tell me if any of this makes sense. Any pointers to relevant projects/articles will be much appreciated. Philip Stoev http://www.stoev.org/pivot/manifest.htm ...
0
by: Mike Chirico | last post by:
Interesting Things to Know about MySQL Mike Chirico (mchirico@users.sourceforge.net) Copyright (GPU Free Documentation License) 2004 Last Updated: Mon Jun 7 10:37:28 EDT 2004 The latest...
4
by: Ross Contino | last post by:
Hello to all: I have been searching the web for examples on how to determine a median value in a mySQL table. I have reviewed the article at...
6
by: Ljoha | last post by:
I need to create report where will be shown total quantity of all rows for every hour in some data range. I have a table where I have column in DATETIME format. I have problem to create sql...
1
by: jlee | last post by:
I'm pretty much a newbie on mysql, and I need some help. I am running mysql Ver 12.22 Distrib 4.0.24, for portbld-freebsd5.4 (i386) on a server hosting an active website. The site's developer...
14
by: dottty | last post by:
Hi, i have a table that has the following fields: id, name, dept, pay 1, John, Sales, 4000 2, Peter, HR, 5000 etc. How do i count how many people there are in each dept with an sql query? ...
6
by: ojorus | last post by:
Hi! My company make several flash-based games, and I use php to communicate with mysql to provide highscore-lists. My problem is this: When I save a player's score in the mysql-table, I want to...
27
by: gerrymcc | last post by:
Hello, I'm a php/mysql beginner... Is there any way of making the mysql command line client full-screen? Sometimes it's easier to use the client than go thru php, but since it's only about 80...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.