Jump to content

[SOLVED] 2 table, need best way to avoid too much queries


asmith

Recommended Posts

Hey guys

 

I want to echo tabular data on an html with while loop.  (mysql_fetch_array)

 

2 of the fields in this html table, are the tab information in another mysql table. for example : 

 

html table  :

name    site    members_num  special_members

 

ocean  site1  7                    john

sea      site2  4                    alex, tom

 

 

there's a table : groups which contains the name field and site field.

there's a table : members which contains those names and a group field which by that I find out in which group that members is .

 

the code I try to avoid is this : (because of making too much queries)

 

$query = mysql_query ("select * from GROUPS");

while ($the_group = mysql_fetch_array($query))

{

$res = mysql_query("select * from MEMBERS where group_field= $the_group[name]");

 

mysql_num_rows($res) --> for html table members_num column.

 

}

 

my point is . if the first query returns 100 rows, then in my while loop it does 100 queries and that's a lot.

 

Any idea?

 

Thanks

 

 

Link to comment
Share on other sites

Try and play with something like this to see if you can get the exact result set you're looking for.

 

select a.name. a.site, count(b.member_id), group_concat(b.member_name SEPARATOR ',') from groups a left outer join members b on b.group_id = a.id group by a.id

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.